Since I recently deal with databases, I need to use C # and SQL Server 2005 for operations. I will summarize the most common operations in the recent period. I am also the first time to use C # To operate databases, so these three typical usage are quite helpful for beginners.
The following is a Class I wrote on Visual Studio 2005 (connected to SQL Server 2005), which has passed the test. There are three typical methods, and the source code is pasted here:
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. Data. sqlclient;
Namespace databaseoperate
{
Class sqloperateinfo
{
// Suppose your servername is "AA", databasename is "BB", username is "cc", password is "DD"
Private string sqlconnectioncommand = "Data Source = AA; initial catalog = BB; user id = cc; Pwd = dd ";
// This table contains two columns: keywordid int not null, keywordname varchar (100) not null
Private string datatablename = "basic_keyword_test ";
Private string storedprocedurename = "sp_inerttobasic_keyword_test ";
Private string sqlselectcommand = "select keywordid, keywordname from basic_keyword_test ";
// Sqlupdatecommand cocould contain "insert", "delete", "Update" operate
Private string sqlupdatecommand = "delete from basic_keyword_test where keywordid = 1 ";
Public void usesqlreader ()
{
Sqlconnection = new sqlconnection (sqlconnectioncommand );
Sqlcommand = new sqlcommand ();
Sqlcommand. commandtype = system. Data. commandtype. text;
Sqlcommand. Connection = sqlconnection;
Sqlcommand. commandtext = sqlselectcommand;
Sqlconnection. open ();
Sqldatareader = sqlcommand. executereader ();
While (sqldatareader. Read ())
{
// Get keywordid and keywordname, you can do anything you like. Here I just output them.
Int keywordid = (INT) sqldatareader [0];
// The same as: int keywordid = (INT) sqldatareader ["keywordid"]
String keywordname = (string) sqldatareader [1];
// The same as: String keywordname = (INT) sqldatareader ["keywordname"]
Console. writeline ("keywordid =" + keywordid + ", keywordname =" + keywordname );
}
Sqldatareader. Close ();
Sqlcommand. Dispose ();
Sqlconnection. Close ();
}
Public void usesqlstoredprocedure ()
{
Sqlconnection = new sqlconnection (sqlconnectioncommand );
Sqlcommand = new sqlcommand ();
Sqlcommand. commandtype = commandtype. storedprocedure;
Sqlcommand. Connection = sqlconnection;
Sqlcommand. commandtext = storedprocedurename;
Sqlconnection. open ();
Sqlcommand. executenonquery ();
// You can use reader here, too. As long as you modify the SP and let it like select * from ....
Sqlcommand. Dispose ();
Sqlconnection. Close ();
}
Public void usesqldataset ()
{
Sqlconnection = new sqlconnection (sqlconnectioncommand );
Sqlcommand = new sqlcommand ();
Sqlcommand. commandtype = system. Data. commandtype. text;
Sqlcommand. Connection = sqlconnection;
Sqlcommand. commandtext = sqlselectcommand;
Sqlconnection. open ();
Sqldataadapter = new sqldataadapter ();
Sqldataadapter. selectcommand = sqlcommand;
Dataset dataset = new dataset ();
// Sqlcommandbuilder is for update the dataset to database
Sqlcommandbuilder = new sqlcommandbuilder (sqldataadapter );
Sqldataadapter. Fill (dataset, datatablename );
// Do something to dataset then you can update it to database. Here I just add a row
Datarow ROW = dataset. Tables [0]. newrow ();
Row [0] = 10000;
Row [1] = "new row ";
Dataset. Tables [0]. Rows. Add (ROW );
Sqldataadapter. Update (dataset, datatablename );
Sqlcommand. Dispose ();
Sqldataadapter. Dispose ();
Sqlconnection. Close ();
}
}
}
The above Program The most typical usage is also the most basic usage. more usage I will give one after another, do you have any questions or suggestions, welcome to letter (jiangbiao0827@163.com) or leave a message. I will also give some suggestions about SQL server in the database area recently.
Original article: http://www.knowsky.com/345354.html