Use C # To operate SQL Server 2000 databases

Source: Internet
Author: User

Add reference first
Using system. Data;
Using system. Data. sqldata;

String conn_str = "Data Source = Name or IP address of the machine where the database is located; initial catalog = Name of the connected database; user id = user name for connecting to SQL sa; Password = password for connecting to SQL "; // connection string
Sqlconnection conn = new sqlconnection (conn_str );
Sqlcommand comm = new sqlcommand ("SQL statement", Conn );
Comm. Some method ();

 

 

 

Frequently used objects include sqlconnection, sqladapter, sqlcommand, dataset, DataGrid, and datareader. The objects include sqlconnection, sqladapter, sqlcommand, dataset, and DataGrid, the categorys table in the instance database northwind that operates SQL is used as an example (assume that the database is local, the database access username is user, and the password is 123456 ):
First introduce Database Operation Reference:
Using system. Data. sqlclient;
1. 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); // defines a data connection instance
Sqlcommand mycommand = new sqlcommand ("select categoryid, categoryname, description from categories", myconnection); // define a database operation command
Sqldataadapter selectadapter = new sqldataadapter (); // defines a data adapter
Selectadapter. selectcommand = mycommand; // define the Operation Command of the data adapter
Dataset mydataset = new dataset (); // defines a dataset.
Myconnection. open (); // open the database connection
Selectadapter. selectcommand. executenonquery (); // execute the database query command
Myconnection. Close (); // close the database
Selectadapter. Fill (mydataset); // fill the dataset
Datagrid1.datasource = mydataset;
Datagrid1.databind (); // fill the data table with data in the dataset

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
{< br> myconnection. open ();
mycommand. executenonquery ();
myconnection. close ();
}< br> catch (exception ex)
{< br> console. writeline ("{0} exception caught. ", ex);
}

3. modify 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
{< br> myconnection. open ();
mycommand. executenonquery ();
myconnection. close ();
textbox1.text = "";
}< br> catch (exception ex)
{< br> console. writeline ("{0} exception caught. ", ex);
}

4. delete 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 );
}

The queried data is displayed in the DataGrid. You can use several input boxes to add, modify, and delete data. You can refer to msdn:
Http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/vbcon/html/vboricodeexamplesfordataaccess. asp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.