Mega Code Archive

 
Categories / ASP.Net Tutorial / ADO Net Database
 

Execute delete command with SqlCommand against SqlServer

<%@ Page Language="C#" %> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> <script runat="server">     void Page_Load(object sender, EventArgs e) {         string ConnectionString = ConfigurationSettings.AppSettings["MSDEConnectString"];         SqlConnection myConnection = new SqlConnection(ConnectionString);              try{             string CommandText = "DELETE FROM Publisher WHERE PublisherID = 6";             SqlCommand myCommand = new SqlCommand(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>     </form> </body> </html> File: Web.config <configuration>     <appSettings>         <add key="MSDEConnectString" value="server=(local)\YourDatabaseAddress;database=Books;uid=YourID;pwd=letmein;" />     </appSettings> </configuration>