Execcute delete statement : OleDBCommand « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
    void Page_Load(object sender, EventArgs e) {
        string ConnectionString = ConfigurationSettings.AppSettings["AccessConnectString"];
        OleDbConnection myConnection = new OleDbConnection(ConnectionString);
    
        try{
            string CommandText = "DELETE FROM Publisher WHERE PublisherID = 6";
            OleDbCommand myCommand = new OleDbCommand(CommandText, myConnection);
    
            myConnection.Open();
    
            lblRecords.Text = Convert.ToString(myCommand.ExecuteNonQuery());
        } catch (Exception ex){
            throw(ex);
        } finally{
            myConnection.Close();
        }
    }

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        Records affected: <asp:Label id="lblRecords" runat="server"></asp:Label>
        <!-- Insert content here -->
    </form>
</body>
</html>


File: Web.config

<configuration>
    <appSettings>
        <add key="AccessConnectString" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Books.mdb;" />
    </appSettings>
</configuration>








18.12.OleDBCommand
18.12.1.Use OleDBCommand to read data returned from a select statement (C#)
18.12.2.Pass OleDbParameter to OleDbCommand (VB.net)
18.12.3.Call stored procedure and pass in parameters (VB.net)
18.12.4.Execcute delete statement
18.12.5.Create OleDbDataReader object from OleDbCommand