1. Create a connection object for the sqlconnection class
Sqlconnection mysqlconnection =
New sqlconnection ("Server = localhost; database = northwind; uid = sa; Pwd = sa ");
// Server specifies the name of the computer that runs the SQL statement. localhost is a common name.ProgramComputer
2. Create a sqlcommand object
Sqlcommand mysqlcommand = mysqlconnection. createcommand ();
3. Set the commandtext attribute of the sqlcommand object
Mysqlcommand. commandtext =
"Select customerid, companyName, contactname, address" +
"From customer" +
"Where customerid = 'alfki '";
4. Open the sqlconnection object
Mysqlconnection. open ();
5. Run the SELECT statement
Sqldatareader mysqldatareader = mysqlcommad. executereader ();
// Call executereader () to run select and return a sqldatareader object
6. Read rows
Mysqldatareader. Read ();
7. display the column values in the sqldatareader object
Console. writeline ("mysqldatareader [/" customerid/"] =" +
Mysqldatareader ["mermerid"] ");
// The following items are in sequence, such as commp anyname...
8. Close the sqlconnection connection.
Mysqlconnection. Close ();
C # connecting SQL2008-09-18
**************************************** ********************* * Use command to connect to the database * **************************************** ********************* "Server =.; database = student; uid = sa; Pwd = 123456" Sqlconnection connection = new sqlconnection ("Data Source =.; initial catalog = database name; user id = sa; Pwd =") // establish a connection Connection. open (); // enable connection Sqlcommand command = new sqlcommand ("SQL statement", connetction); // occupies the connetction connection and executes the SQL statement String SQL = string. Format ("select * From where Char = '{0}'", cboclass. Text); // you can use this SQL statement. Command. executenocquery (); // execute and return the number of execution rows; Command. executereader (); // execute and return the datareader object Command. executescalar (); // return a single value, for example, execute count (*) Sqldatareader datareader = command. executereader (); // read the connection of command Datareader. Read (); // read, return bool Value (String) datareader [value | column name]; // The value starts from 0. **************************************** ********************* * Use dataset to connect to the database * **************************************** ********************* Dataset mydataset = new dataset ("Database Name"); // defines the data object set without Parameters Sqldataadapter dataadapter = new sqldataadapter (SQL statement, connection); // create a database connection (directly execute) Dataadapter. Fill (dataset, "table name"); // fill Sqlcommandbuilder builder = new sqlcommandbuilder (A created dataadapter object); // automatically generates an SQL statement Dataadapter. Update (dataset, "table name"); // update Dataadapter methods include: Fill submits data to the table in dataset. Update: submit data in dataset to the database. dataadapter transmits data from the database to dataset through connection to use data in dataset, you must use dataadapter dataset. datatable [serial number | table name]. datacolumn dataset. datatable [serial number | table name]. datarow row |