, , .
, , DbConnection.
, :
< cnfiguration>
<appSettings>
<!-- ? -->
<add key="provider" value="System.Data.SqlClient" />
<!-- ? -->
<add key="cnStr" value=
"Data Source=localhost;uid=sa;pwd=;Initial Catalog=Pubs"/> </appSettings>
</configuration>
:
//
//
string dp = ConfigurationManager.AppSettings("provider");
string cnStr = ConfigurationManager.AppSettings("cnStr");
// .
DbProviderFactory df = DbProviderFactories.GetFactory(dp);
//
DbConnection n = df.CreateConnection();
cn.CornneccionString = cnStr;
cn.Open();
, Connection. CommandType, StoredProcedure, TableDirect, Text.
//
DbCommand cmd = df.CreateComnmand();
cmd.Connection = cn;
cmd.CommandText = "Select * From Authors";
, , , .
SQL- . DbDataReader ( IDataReader) . , . , SQL- .
, . , 20000 , , DataSet . , , . , ( , ) , .
|
|
ExecuteReader (). , CommandBehavior. CloseConnection.
, , , Read() .
//
DbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
Console.WriteLine("{0}, {1}", dr[au.lname"], dr[au.fname");
// CommandBehavior.CloseConnection,
// Close()
dr.Close();
, , , ExecuteNonQuery. , .
, ADO.NET . , , "" SQL-. SQL- , SQL- @ ( , , Microsoft SQL Server, ).
string sql = string.Format("Insert Into Inventory" + "(CarlD, Make, Color, PetName, Values" + "(@CarID, @Make, @Color, @PetName)");
//
SqlParameter param = new SqlParameter();
param.ParameterName = "@ID";
param.Value = newCarID;
param.SqlDbType = SqlDbType.Int;
cmd.Parameters.Add(param);