SQL Server C # connection Summary

Source: Internet
Author: User

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

Related Article

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.