Recently in the company has been useful to the ADO technology, a review of the way down.
, water is like a reservoir, into the faucet like connection, the same, pump: Command, water pipe: DataAdapter or DataReader. DataAdapter is understood as water directly from the pipe to the reservoir, while the DataReader is understood as water directly to the pipe used by the user. DataReader is faster than direct, because the user can use it directly. DataSet: Big Reservoir, DataTable: Pond of the Big reservoir.
Practice a Connection
Using system.data.sqlclient;//reference namespaces
Note: The value of the ConnectionState enumeration
| Enumeration values |
Description |
| Broken |
The connection to the data source was interrupted. Can occur only after a connection. |
| Closed |
The connection is in a closed state |
| Connecting |
Connection object is connecting to the data source |
| Executing |
The connection object is executing the command |
| Fetching |
The connection object is retrieving data |
| Open |
Connection Status is open |
Example 1,
Figure 1-1
Try
{
String Con = "server=.; Database= "+ textBox1.Text.Trim () +"; uid=sa;pwd= ";//1-1 shows that the corresponding
SqlConnection coon = new SqlConnection (Con);
Coon. Open ();
if (Coon. state = = ConnectionState.Open)//Determine connection status
Label2. Text = "Connection succeeded";
}
Catch
{
Label2. Text = "Connection Failed";
}
Run
Example 2,
Closed () method, Dispose () method.
Coon. Close (); Use Coon. Open (); You can reconnect the database
Coon. Dispose (); Use Coon. Open (); You cannot reconnect to the database. The ConnectionString property has not been initialized for the exception.
Detailed Command because the pump, the most important machine power
The Command object has 3 important properties. The Connection,commandtext,commandtype are respectively.
The Connection is used to set the SQLConnection used by SqlCommand.
The CommandText is used to set the SQL statement or stored procedure to be executed against the data source.
The CommandType is used to set the CommandText type, typically text:sql text commands.
example, explain the use of the three attributes.
String Con = "server=.; Database=movies;uid=sa;pwd= ";
SqlConnection coon = new SqlConnection (Con);
Coon. Open ();
SqlCommand cmd = new SqlCommand ();//Create a SqlCommand object
Cmd. Connection = coon;//Setting Connection property
Cmd.commandtext = "SELECT * from Movies";//Set SQL statement for CommandText
Cmd.commandtype = CommandType.Text; Set the CommandType property to text so that it executes only the literal form of the SQL statement.
Label2. Text = (cmd. ExecuteScalar ()). ToString ();//executescalar () method gets the first column of data in the first row of the data table.
Run Result: Specifies the data, specifying the first row of data in the first column of the data table.
The above is a brief explanation of the basis, in the ADO (b), specifically explain the command of some of the expansion and extension.
ADO (a)