Click here to Skip to main content
15,561,817 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
sql connectivity in asp.net
Posted

 
Share this answer
 
Here is a simple example of connectivity :

Note : Make the tables in SQL Server Management Studio.


protected void btnsubmit_Click(object sender, EventArgs e)
    {
           SqlConnection CON;
           SqlCommand COM;
       CON = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Name"].ConnectionString);
      COM = new SqlCommand("INSERT INTO TableName (ID, Name, Address) VALUES (@ID, @Name, @Address)", CON);
            COM.Parameters.Add("@ID", System.Data.SqlDbType.NChar,10);
            COM.Parameters["@ID"].Value = txtid.Text;
            COM.Parameters.Add("@Name", System.Data.SqlDbType.NChar,50);
            COM.Parameters["@Name"].Value = txtname.Text;
            COM.Parameters.Add("@Address", System.Data.SqlDbType.NChar,100);        
            COM.Parameters["@Address"].Value = txtaddress.Text;       
            CON.Open();    
        COM.ExecuteNonQuery();
        Response.Redirect("Default.aspx");       
        //CON.Close();

    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
          SqlConnection CON;
           SqlCommand COM;
       CON = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_Name"].ConnectionString);
      //COM = new SqlCommand("update TableName set id='"+txtid.Text+"',Name='"+txtname.Text+"',Address='"+txtaddress.Text+"'");
       COM = new SqlCommand("Update <b>TableName</b>set Name=@Name,Address=@Address where ID=@Id",CON);
       COM.Parameters.Add("@ID", System.Data.SqlDbType.NChar, 10);
       COM.Parameters["@ID"].Value = txtid.Text;
       COM.Parameters.Add("@Name", System.Data.SqlDbType.NChar, 50);
       COM.Parameters["@Name"].Value = txtname.Text;
       COM.Parameters.Add("@Address", System.Data.SqlDbType.NChar, 100);
       COM.Parameters["@Address"].Value = txtaddress.Text;
       CON.Open();
       COM.ExecuteNonQuery();
       Response.Redirect("Default.aspx");
       CON.Close();
        
      }
    protected void btndelete_Click(object sender, EventArgs e)
    {
        SqlConnection CON;
        SqlCommand COM;
        CON = new SqlConnection(ConfigurationManager.ConnectionStrings["DataBaseName"].ConnectionString);
        COM = new SqlCommand("Delete from TableName where ID=@ID",CON);
        COM.Parameters.Add("@ID", System.Data.SqlDbType.NChar, 10);
        COM.Parameters["@ID"].Value = txtid.Text;
        COM.Parameters.Add("@Name", System.Data.SqlDbType.NChar, 50);
        COM.Parameters["@Name"].Value = txtname.Text;
        COM.Parameters.Add("@Address", System.Data.SqlDbType.NChar, 100);
        COM.Parameters["@Address"].Value = txtaddress.Text;
        CON.Open();
        COM.ExecuteNonQuery();
        Response.Redirect("Default.aspx");
        CON.Close();
    }




Add a connection String in Web.Config

XML
<connectionStrings>
        <add name="DataBaseName" connectionString="Server=ADMIN-PC;Database=DataBaseName;Integrated Security=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>
 
Share this answer
 
v2
Did you search on the internet? I found a huge number of links that you could start with. Or better start reading a book on ASP.Net DB Connectivity.

Some links that I found -
http://www.w3schools.com/aspnet/aspnet_dbconnection.asp[^]
http://msdn.microsoft.com/en-us/library/ff648340.aspx[^]
 
Share this answer
 
Comments
lovejeet0707 28-Jun-11 2:31am    
He is asking for sql connection but link of w3schools is based on oledb connection.......

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900