Creating Sql Database Connection in Asp.net Web Application Through C#
1: Open visual studio 2005 and from FILE select NEW-> WebSite, then from pop-menu select asp.net website (Note: Language must be C#) , then press OK.
?
2: Now create three text from the toolbox in order to eliminate ambiguity, I use the default name of the text box and should be TextBox1's, TextBox2 in
?
3: At the same time, open SQL Server Management Studio 2005 and open the AdventureWorks (or you can SQL commands CREATE DATABASE database_name's own database), but here I use the AdventureWorks database.
?
4: Then create by clicking the right mouse button and make three columns, name it corresponds to your needs, just for your information I have EID, name, address. Click and click Save, then you will get a pop-up menu, where you enter the table name, for this example, I have students.
?
5: Now Come Back To Visual Studio application and write the following code in the event Button_Click
?
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = “Connection string(See Below for this — )COPY PASTE? HERE“;
myConn.Open();
string strqry = “Insert into students values (” + TextBox1.Text +
“,’” + TextBox2.Text + “‘,’” + TextBox3.Text + “‘)”;
SqlCommand myCom = new SqlCommand(strqry, myConn);
int numrow = myCom.ExecuteNonQuery();
myConn.Close();
}
Kindly note down the connectionString Syntax, to obtain this string do the following steps :
Step 1 : Go to toolbox and from data section drag & drop SqlDataSource.
Receive Step 2: Then on the left-mouse click on SqlDataSource, you configureDataSource link Slect them.
Step 3: NewConnection string –
Step 4: TestConnection and then Press OK.
Step five: Now you will again be redirected to a web page to see the actual step 2, but the connection string, and from the drop-down flood level, you will receive the connection string when you click login.
Step 6: Just copy and paste this string to the place of connectionstring.
Now debug & run Your application and check it’s effect on the table.
If still you face any problem feel free to ask.
Hope this explanation help you to understand the basics of database connectivity.
If you like this please comment it !
Thanks,
Anuj
