1. Use SqlConnection to connect to SQL Server
1. Add a namespace
Using System. Data. SqlClient;
2. Connect to the database
SqlConnection myConnection = new SqlConnection ();
MyConnection. C;
MyConnection. Open ();
Improved (more general) methods:
String MySqlC;
SqlConnection myConnection = new SqlConnection (MySqlConnection );
MyConnection. Open ();
II. Use oledbconnection to connect
1. Add a namespace
Using System. Data. OleDb;
2. Connect to SQL Server
String MySqlC;
SqlConnection myConnection = new SqlConnection (MySqlConnection );
MyConnection. Open ();
3. Connect to access (strings can be obtained by creating a. udl file)
String MySqlCProvider = MSDAORA; Data Source = db; user id = sa; password = sinofindb ";
3. Create a command object
1. sqlcommand Constructor
① Initialize a new instance of the SqlCommand class. Public SqlCommand ();
SqlCommand myCommand = new SqlCommand ();
② Initialize a new instance of the SqlCommand class with query text. Public SqlCommand (string );
String mySelectQuery = "Select * FROM mindata ";
Sqlcommand mycommand = new sqlcommand (myselectquery );
③ Initialize the sqlcommand class instance with query text and sqlconnection.
Public sqlcommand (string, sqlconnection );
String myselectquery = "select * From mindata ";
String ACC;
Sqlconnection myconnection = new sqlconnection (myconnectstring );
Sqlcommand mycommand = new sqlcommand (myselectquery, myconnection );
④ Initialize the sqlcommand class instance with query text, sqlconnection, and transaction.
Public sqlcommand (string, sqlconnection, sqltransaction );
Sqltransaction mytrans = myconnection. begintransaction ();
String myselectquery = "select * From mindata ";
String ACC;
Sqlconnection myconnection = new sqlconnection (myconnectstring );
Sqlcommand mycommand = new sqlcommand (myselectquery, myconnection, mytrans );
2. Establish an association between sqlcommand and sqlconnection.
Mycommand. Connection = myconnection;
Or: sqlcommand mycommand = myconnection. createcommand;
3. Set the query text of sqlcommand.
Mycommand. commandtext = "select * From mindata ";
Or 2nd types of structures: sqlcommand mycommand = new sqlcommand (myselectquery );
The sqlcommand object provides two query strings. Each query string Accesses Different tables and returns different result sets.
The two query statements are separated by semicolons.
4. Run the command.
Executereader
Returns one or more rows.
Executenonquery
Execute a Transact-SQL statement on the connection and return the affected number of rows (INT)
Executescalar
Returns a single value (such as an aggregate value). returns the first column of the first row of the result set. Ignore extra columns or rows
Executexmlreader
Send commandtext to connection and generate an xmlreader object.
Sqldatareader myreader = mycommand. executereader ();
Or sqldatareader myreader = mycommand. executereader (commandbehavior. closeconnection );
While (myreader. Read () // read data cyclically
{
Console. writeline (myreader. getstring (0); // obtain the value in the string format of the specified Column
Console. writeline (myreader. getvalue (1); // obtain the value of a specified column in the local format
}
Commandtext = "select count (*) as numberofregions from Region ";
Int COUNT = (INT) mycommand. executescalar ();
The use of oledbcommand objects.
4. Use of datareader
1. traverse the result set
While (myreader. Read ())
Console. writeline ("\ t {0} \ t {1}", myreader. getint32 (0), myreader. getstring (1 ));
Myreader. Close ();
2. Use the ordinal index.
While (myreader. Read ())
Console. writeline ("\ t {0} \ t {1}", myreader [0]. tostring (), myreader [1]. tostring ());
Myreader. Close ();
3. Use the column name indexer.
While (myreader. Read ())
Console. writeline ("\ t {0} \ t {1}", myreader ["Code]. tostring (), myreader [" name "]. tostring ());
Myreader. Close ();
4. Use the type accessors.
Public char getchar (int I); returns a single string value of the specified column.
Public datetime getdatetime (int I); get the value in the datetime format of the specified Column
Public short GetInt16 (int I); get the 16-bit signed integer form of the specified column [C #]
Public string GetString (int I); get the value in the string format of the specified Column
5. Obtain the column information.
MyReader. FieldCount gets the number of columns in the current row
MyReader. GetFieldType (serial number) is the Type of the object's data Type.
MyReader. GetDataTypeName (serial number) Get the name of the source data type
MyReader. GetName (serial number) to get the name of the specified Column
MyReader. GetOrdinal (sequence number) obtains the sequence number of a given column name.
6. Obtain the data table information.
MyReader. GetSchemaTable () returns a DataTable
7. Operate multiple result sets.
MyReader. NextResult () enables the data reader to move forward to the next result set
Do
{
While (myReader. Read ())
Console. WriteLine ("\ t {0} \ t {1}", myReader. GetInt32 (0), myReader. GetString (1 ));
}
While (myReader. NextResult ());
5. dataadapter
1. Create SqlDataAdapter
Initialize a new instance of the SqlDataAdapter class.
Public SqlDataAdapter ();
Use the specified SqlCommand as the SelectCommand attribute to initialize a new instance of the SqlDataAdapter class.
Public SqlDataAdapter (SqlCommand );
Use the selectcommand string and SqlConnection object to initialize a new instance of the SqlDataAdapter class.
Public SqlDataAdapter (string, SqlConnection );
Use the selectcommand string and a connection string to initialize a new instance of the SqlDataAdapter class.
Public SqlDataAdapter (string, string );
2. Create an association between DataAdapter, SqlConnection, and SqlCommand.
1. DataAdapter is created when constructing Parameters
2. SqlDataAdapter adapter = new SqlDataAdapter ();
Adapter. SelectCommand = new SqlCommand (query, conn );
3. DataAdapter. Fill () method.
Add or refresh rows in DataSet to match the rows in the data source with the DataSet name, and create a DataTable named "Table.
Public override int Fill (DataSet );
Add or refresh rows in DataSet to match the rows in the data source that uses the DataSet and able names.
Public int Fill (DataSet, string );
Add or refresh rows in the specified range of DataSet to match the rows in the data source using the DataSet and DataTable names.
Public int Fill (DataSet, int, int, string );
Add or refresh rows in DataTable to match rows in the data source that uses the DataTable name.
Public int Fill (DataTable );
Add or refresh rows in the DataTable to match the rows in the data source that uses the specified DataTable and IDataReader names.
Protected virtual int Fill (DataTable, IDataReader );
Add or refresh rows in the DataTable to match rows in the data source that uses the DataTable name, specified SQL Select statement, and CommandBehavior.
Protected virtual int Fill (DataTable, IDbCommand, CommandBehavior );
6. datatable class
VII. datacolumn class
8. datarow class
9. dataset class
1. Create a DataSet object
Initializes a new instance of the DataSet class.
Public DataSet ();
Initialize a new instance of the DataSet class with the given name.
Public DataSet (string );
2. Fill DataSet with DataAdapter
DataSet myds = new DataSet ();
Adapter. fill (myds)
Adapter. fill (myds, "table name"); fill DataSet with a table.
10. datatablecollection class. Indicates the set of DataSet tables.
DataTableCollection dtc = ds. Tables;
DataTable table = dtc ["table name"];
String strExpr = "id> 5 ";
String strSort = "name DESC ";
DataRow [] foundRows = customerTable. Select (strExpr, strSort ,);
Dynamic Filtering and sorting.
DataTable. Select () method: Get the array of the DataRow object,
① Obtain the array of all DataRow objects.
Public DataRow [] Select ();
② Obtain the array of all DataRow objects that match the filtering conditions in the primary key order (if no primary key exists.
Public DataRow [] Select (string );
③ Obtain the array of all DataRow objects in the specified sorting order that matches the filtering conditions.
Public DataRow [] Select (string, string );
④ Obtain the array of all DataRow objects that match the filters and specified states in the sorting order.
Public DataRow [] Select (string, string, DataViewRowState );
11. Dataview class: A dynamic view of the DataTable content.
1. Create an object
Initialize a new instance of the DataView class.
Public DataView ();
Use the specified datatable to initialize a new instance of the dataview class.
Public dataview (datatable );
Use the specified able, rowfilter, sort, and dataviewrowstate to initialize a new instance of the dataview class.
Public dataview (datatable, String, String, dataviewrowstate );
Dataview myview = new dataview (Ds. Tables ["Suppliers"], "id> 5", "Name DESC ",
Dataviewrowstate. currentrows );
2. Obtain the row data of dataview.
Foreach (datarowview myrowview in myview)
{
For (INT I = 0; I <myview. Table. Columns. Count; I ++)
Console. Write (myrowview+ "\ T ");
Console. writeline ();
}