Command
对象中包含了提交给数据库系统的访问信息
OleDbCommand与SqlCommand对象,它们的基本对象与操作方法是相同的,在此仅介绍OleDbCommand的用法,SqlCommand的用法类推即可。
最常用初始化Command对象的方法是在建立Command实例中带入两个参数
OleDbCommand myComm=new OleDbCommand(strQuery,myConnection);
strQuery为SQL语句或储存过程名,myConnection是建立的Connection对象实例
Command对象主要的属性有
Connection:
CommandText:设置或获取Command对象使用的Connection对象实例
CommandType:StoredProceduce、TableDirect、Text(标准SQL语句)
Parameters:取得参数值集合
ExecuteReader:执行CommandText指定的SQL语句或储存过程名,返回类型为DataReader
ExecuteNonQuery:与ExecuteReader功能相同,只是返回值为执行SQL语句或储存过程受影响的记录行数
OleDbCommand与SqlCommand对象,它们的基本对象与操作方法是相同的,在此仅介绍OleDbCommand的用法,SqlCommand的用法类推即可。
最常用初始化Command对象的方法是在建立Command实例中带入两个参数
OleDbCommand myComm=new OleDbCommand(strQuery,myConnection);
strQuery为SQL语句或储存过程名,myConnection是建立的Connection对象实例
Command对象主要的属性有
Connection:
CommandText:设置或获取Command对象使用的Connection对象实例
CommandType:StoredProceduce、TableDirect、Text(标准SQL语句)
Parameters:取得参数值集合
ExecuteReader:执行CommandText指定的SQL语句或储存过程名,返回类型为DataReader
ExecuteNonQuery:与ExecuteReader功能相同,只是返回值为执行SQL语句或储存过程受影响的记录行数
DataReader--SqlDataReader、OleDbDataReader
FieldCount:显示当前操作记录的字段总和
IsClosed:判断DataReader是否关闭
Close:关闭DataReader
GetString:以String返回指定列中的值
GetValue:以自身的类型返回指定列中的值
GetValues:返回当前记录所有字段的集合
Read:读下一条记录
例:
FieldCount:显示当前操作记录的字段总和
IsClosed:判断DataReader是否关闭
Close:关闭DataReader
GetString:以String返回指定列中的值
GetValue:以自身的类型返回指定列中的值
GetValues:返回当前记录所有字段的集合
Read:读下一条记录
例:
<%
@PageLanguage
=
"
C#
"
%>
<% @ImportNamespace = " System.Data " %>
<% @ImportNamespace = " System.Data.OleDb " %>
-------------------------------------------
<% @ImportNamespace = " System.Data.SqlClient " %>
< ScriptLanguage = " c# " Runat = " Server " >
OleDbDataReaderdr;
Stringmyconnstring = " Provider=Microsoft.Jet.OLEDB.4.0;DataSource= " + Server.MapPath( " . " ) + " ..\mdb " ;
string sql = " .. "
OleDbConnectionmy_conn = new OleDbConnection(myconnstring);
OleDbCommandmy_comm = new OleDbCommand(sql,my_conn);
my_comm.Connection.Open();
dr = my_comm.ExecuteReader();
// DataGriddg
dg.DataSource = dr
dg.DataBind();
</ Script >
< head >
<% while (dr.Read())
{;}
%>
</ head >
DataSetDataAdapter
DataAdapter是与DataSet配套使用的
OleDbDataAdaptermy_adapter = new OleDbDataAdapter();
-------------------------------------------------
SqlDataAdaptermy_sqladapter = new SqlDataAdapter();
my_adapter.SelectCommand.Connection = my_conn;
或
my_adapter.UpdateCommand.Connection = my_conn;
my_adapter.DeleteCommand.Connection = my_conn;
my_adapter.InsertCommand.Connection = my_conn;
如果需要执行Sql语句,那么还必须给相应的CommandText属性赋值。
my_adapter.SelectCommand.CommandText = strSelect;
my_adapter.UpdateCommand.CommandText = strUpdate;
my_adapter.InsertCommand.CommandText = strInsert;
my_adapter.DeleteCommand.CommandText = strDelete;
如果只是查询数据库,则可以建立DataAdapter实例时就完成上述工作
OleDbDataAdapterMyAdapter = new OleDbDataAdapter(sql,my_conn);
DataSetds = new DataSet();
OleDbConnectionmy_conn = new OleDbConnection(myconnstring);
OleDbDataAdaptermy_adapter = new OleDbDataAdapter(sql,my_conn);
my_adapter.fill(ds, " score " );
DataGridscore;
score.DataSource = ds.Table[ " score " ].Defaultview;
score.DataBind();
// InsertCommand
DataRowdr = ds.Table[ " Score " ].NewRow();
dr[ " name " ] = ..
ds.Tables[ " Score " ].Rows.Add(dr);
// update
DataAdapter.Update(ds, " Score " );
// modify
DataRowdr = ds.Tables[ " Score " ].Rows[ 0 ];
dr[ " name " ] = ;
// Delete
DataRowdr = ds.Talbes[ " Score " ].Rows[ 0 ];
dr.Delete();
// 注:
OleDbConnectionmy_conn = new OleDbConnection(myconnstring);
OleDbDataAdaptermy_adapter = new OleDbDataAdapter(sql,my_conn);
OleDbCommandBuildercustCB = new OleDbCommandBuilder(my_adapter); // DataAdapter在数据发生改变时,并不能自动产生(Transact-SQL),所以一定要使用CommandBuilder,它能自动产生Sql语句,!就此一步
RejectChanges是DataSet,DataTable,DataRow共有的方法
使操作对象拒绝已经发生的改变,使数据复员
if (DataSet.HasErrors)
{
DataSet.RejectChanges();
}
else {DataSet.AcceptChanges();}
----------------------------------------------------------------------------------------------------------------------------------
DataReader -> DataTable
public static DataTableConvertDataReaderToDataTable(SqlDataReaderdataReader)
{
/**/ /// 定义DataTable和模式
DataTabledatatable = new DataTable();
DataTableschemaTable = dataReader.GetSchemaTable(); // 导入一个数据表的表结构
try
{ /**/ /// 动态添加表的数据列
foreach (DataRowmyRow in schemaTable.Rows)
{
DataColumnmyDataColumn = new DataColumn();
myDataColumn.DataType = myRow.GetType();
myDataColumn.ColumnName = myRow[ 0 ].ToString();
datatable.Columns.Add(myDataColumn);
}
/**/ /// 添加表的数据
while (dataReader.Read())
{
DataRowmyDataRow = datatable.NewRow();
for ( int i = 0 ;i < schemaTable.Rows.Count;i ++ )
{
myDataRow[i] = dataReader[i].ToString();
}
datatable.Rows.Add(myDataRow);
myDataRow = null ;
}
schemaTable = null ;
/**/ /// 关闭数据读取器
dataReader.Close();
return datatable;
}
catch (Exceptionex)
{
/**/ /// 抛出类型转换错误
SystemError.SystemLog(ex.Message);
throw new Exception(ex.Message,ex);
}
}
}
}
<% @ImportNamespace = " System.Data " %>
<% @ImportNamespace = " System.Data.OleDb " %>
-------------------------------------------
<% @ImportNamespace = " System.Data.SqlClient " %>
< ScriptLanguage = " c# " Runat = " Server " >
OleDbDataReaderdr;
Stringmyconnstring = " Provider=Microsoft.Jet.OLEDB.4.0;DataSource= " + Server.MapPath( " . " ) + " ..\mdb " ;
string sql = " .. "
OleDbConnectionmy_conn = new OleDbConnection(myconnstring);
OleDbCommandmy_comm = new OleDbCommand(sql,my_conn);
my_comm.Connection.Open();
dr = my_comm.ExecuteReader();
// DataGriddg
dg.DataSource = dr
dg.DataBind();
</ Script >
< head >
<% while (dr.Read())
{;}
%>
</ head >
DataSetDataAdapter
DataAdapter是与DataSet配套使用的
OleDbDataAdaptermy_adapter = new OleDbDataAdapter();
-------------------------------------------------
SqlDataAdaptermy_sqladapter = new SqlDataAdapter();
my_adapter.SelectCommand.Connection = my_conn;
或
my_adapter.UpdateCommand.Connection = my_conn;
my_adapter.DeleteCommand.Connection = my_conn;
my_adapter.InsertCommand.Connection = my_conn;
如果需要执行Sql语句,那么还必须给相应的CommandText属性赋值。
my_adapter.SelectCommand.CommandText = strSelect;
my_adapter.UpdateCommand.CommandText = strUpdate;
my_adapter.InsertCommand.CommandText = strInsert;
my_adapter.DeleteCommand.CommandText = strDelete;
如果只是查询数据库,则可以建立DataAdapter实例时就完成上述工作
OleDbDataAdapterMyAdapter = new OleDbDataAdapter(sql,my_conn);
DataSetds = new DataSet();
OleDbConnectionmy_conn = new OleDbConnection(myconnstring);
OleDbDataAdaptermy_adapter = new OleDbDataAdapter(sql,my_conn);
my_adapter.fill(ds, " score " );
DataGridscore;
score.DataSource = ds.Table[ " score " ].Defaultview;
score.DataBind();
// InsertCommand
DataRowdr = ds.Table[ " Score " ].NewRow();
dr[ " name " ] = ..
ds.Tables[ " Score " ].Rows.Add(dr);
// update
DataAdapter.Update(ds, " Score " );
// modify
DataRowdr = ds.Tables[ " Score " ].Rows[ 0 ];
dr[ " name " ] = ;
// Delete
DataRowdr = ds.Talbes[ " Score " ].Rows[ 0 ];
dr.Delete();
// 注:
OleDbConnectionmy_conn = new OleDbConnection(myconnstring);
OleDbDataAdaptermy_adapter = new OleDbDataAdapter(sql,my_conn);
OleDbCommandBuildercustCB = new OleDbCommandBuilder(my_adapter); // DataAdapter在数据发生改变时,并不能自动产生(Transact-SQL),所以一定要使用CommandBuilder,它能自动产生Sql语句,!就此一步
RejectChanges是DataSet,DataTable,DataRow共有的方法
使操作对象拒绝已经发生的改变,使数据复员
if (DataSet.HasErrors)
{
DataSet.RejectChanges();
}
else {DataSet.AcceptChanges();}
----------------------------------------------------------------------------------------------------------------------------------
DataReader -> DataTable
public static DataTableConvertDataReaderToDataTable(SqlDataReaderdataReader)
{
/**/ /// 定义DataTable和模式
DataTabledatatable = new DataTable();
DataTableschemaTable = dataReader.GetSchemaTable(); // 导入一个数据表的表结构
try
{ /**/ /// 动态添加表的数据列
foreach (DataRowmyRow in schemaTable.Rows)
{
DataColumnmyDataColumn = new DataColumn();
myDataColumn.DataType = myRow.GetType();
myDataColumn.ColumnName = myRow[ 0 ].ToString();
datatable.Columns.Add(myDataColumn);
}
/**/ /// 添加表的数据
while (dataReader.Read())
{
DataRowmyDataRow = datatable.NewRow();
for ( int i = 0 ;i < schemaTable.Rows.Count;i ++ )
{
myDataRow[i] = dataReader[i].ToString();
}
datatable.Rows.Add(myDataRow);
myDataRow = null ;
}
schemaTable = null ;
/**/ /// 关闭数据读取器
dataReader.Close();
return datatable;
}
catch (Exceptionex)
{
/**/ /// 抛出类型转换错误
SystemError.SystemLog(ex.Message);
throw new Exception(ex.Message,ex);
}
}
}
}