2011年8月19日星期五

Creating Sql Database Connection in Asp.net Web Application Through C#




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 reception OK.

 

2: Now create 3 textboxes from the toolbox , to remove ambiguity I am using default name for that textboxes and that should be textbox1,textbox2 & textbox3. And too drag & drop the button to the asp.web application.

 

3: Meanwhile, Open SQL SERVER Management studio 2005 and open adventureworks(or you tin build your own database by the Sql directive CREATE DATABASE database_name), but here I am using AdventureWorks database.

 

4: Then create table by clicking right mouse button and form three column, Name them according to your prerequisite , for your information merely I accustomed EID,name,Address. And click retention button, then you’ll get a pop up menu where you have to enter the Table Name, for this instance I used  students.


 

5: Now Come Back to Visual studio Application and write the following code in the button_click event

 

defended void Button1_Click(thing sender, EventArgs e)
{
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = “Connection string(See Below as this —
)COPY PASTE  HERE“;

myConn.Open();
string strqry = “Insert into students merits (” + TextBox1.Text +
“,’” + TextBox2.Text + “‘,’” + TextBox3.Text + “‘)”;

SqlCommand myCom = fashionable SqlCommand(strqry, myConn);
integer numrow = myCom.ExecuteNonQuery();
myConn.Close();

}


Kindly memorandum down the connectionString Syntax, to get this string do the emulating steps :
Step 1 : Go to toolbox and from data partition tow & drop SqlDataSource.
Step 2: Then ashore left clicking the mouse above SqlDataSource , you ambition obtain configureDataSource interlock, Slect this.
Step 3: NewConnection String -> Provide Server Name then elect or write database appoint , i.e. AdventureWorks.
Step 4: TestConnection and then Press OK.
Step 5 : Now you will again redirect to the sheet that you really discern in the step 2, but this period click + sign ahead the connection string and from the drip drown hierarchy you will receive the connection string.
Step 6: Just copy and paste this string to the area of connectionstring.
Now debug & escape Your application and check it’s efficacy on the chart.
If still you face any problem feel free to inquire.
Hope this explanation assist you to understand the basics of database connectivity.
If you favor this please annotate it !
Thanks,
Anuj




没有评论:

发表评论