1. Adding references
Using System.Data;
Using System.Data.SqlData;
2, established in the database connection, it is recommended to make it a method, convenient multiple use.
String SqlConnection = "Data Source =" Note 1 ";d atabase = Annotation 2;uid =" NOTE 3 ";p WD =" Note 4 "; (There are many other ways)
can refer to http://www.cnblogs.com/delphinet/archive/2011/06/24/2088765.html
3, frequently used objects are: Sqlconnection,sqladapter,sqlcommand, datasets, DataGrid and DataReader, to Sqlconnection,sqladapter, SqlCommand, Dataset, DataGrid object, instance database operation SQL Categorys table in Northwind (assuming the database is local, the user name of the database access is username, password is 123456):
Let's do the following four implementations
First, query data:
String myconn= "Server=127.0.0.1;uid=user;pwd=123456;database=northwind; Trusted_connection=no ";//define Database connection parameters
SqlConnection myconnection=new SqlConnection (myconn);//define a data connection instance
SqlCommand mycommand=new SqlCommand ("Select CategoryID, CategoryName, Description from Categories", MyConnection); Define a database Operation directive
SqlDataAdapter Selectadapter=new SqlDataAdapter ();//define a data adapter
selectadapter.selectcommand=mycommand;//defining operation instructions for data adapters
DataSet mydataset=new DataSet ();//define a data set
Myconnection.open ();//Open database connection
SelectAdapter.SelectCommand.ExecuteNonQuery ();//execute database query instruction
Myconnection.close ();//Close Database
Selectadapter.fill (myDataSet);//Populating data sets
Datagrid1.datasource=mydataset;
Datagrid1.databind ();//Populate data tables with data from data sets
Second, add data
String myconn= "Server=127.0.0.1;uid=user;pwd=123456;database=northwind; Trusted_connection=no ";
SqlConnection myconnection=new SqlConnection (myconn);
String myinsert= "INSERT into Categories (CategoryName, Description) VALUES ('" +convert.tostring (TextBox2.Text) + "', '" + Convert.ToString (TextBox3.Text) + "')";
SqlCommand mycommand=new SqlCommand (myinsert,myconnection);
try//Exception Handling
{
Myconnection.open ();
Mycommand.executenonquery ();
Myconnection.close ();
}
catch (Exception ex)
{
Console.WriteLine ("{0} Exception caught.", ex);
}
Third, modify the data
String Categoryname=textbox2.text;
String Categorydescription=textbox3.text;
String myconn= "Server=127.0.0.1;uid=user;pwd=123456;database=northwind; Trusted_connection=no ";
SqlConnection myconnection=new SqlConnection (myconn);
String myupdate= "Update Categories set categoryname= '" +categoryname+ "', description= '" +categorydescription+ "' where Categoryid= "+textbox1.text;
SqlCommand mycommand=new SqlCommand (myupdate,myconnection);
Try
{
Myconnection.open ();
Mycommand.executenonquery ();
Myconnection.close ();
Textbox1.text= "";
}
catch (Exception ex)
{
Console.WriteLine ("{0} Exception caught.", ex);
}
Iv. deletion of data
String myconn= "Server=127.0.0.1;uid=user;pwd=123456;database=northwind; Trusted_connection=no ";
SqlConnection myconnection=new SqlConnection (myconn);
String mydelete= "Delete from Categories where categoryid=" +textbox1.text;
SqlCommand mycommand=new SqlCommand (mydelete,myconnection);
Try
{
Myconnection.open ();
Mycommand.executenonquery ();
Myconnection.close ();
Textbox1.text= "";
}
catch (Exception ex)
{
Console.WriteLine ("{0} Exception caught.", ex);
}
Add and remove SQL Server in C # (easy to understand)