当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net SqlDataAdapter对象使用札记

ASP.NET
asp.net 无重复随机数代码
asp.net(C#)中上传大文件的几中常见应用方法
asp.net AJAX实现无刷新获得数据
C#中发送邮件代码
asp.net(c#) RSS功能实现代码
asp.net窗体操作总结
ASP.NET 水晶报表打印功能实现代码
ASP.Net 图片存入数据库的实现代码
让Silverlight 2.0动画动起来Making Silverlight 2.0 animation Start(不能运动原因)
asp.net Reporting Service在Web Application中的应用
C# 文件上传 默认最大为4M的解决方法
asp.net 购物车实现详细代码
asp.net repeater实现批量删除时注册多选框id到客户端
asp.net aspnetpager分页统计时与实际不符的解决办法
iis 服务器应用程序不可用的解决方法
asp.net button 绑定多个参数
asp.net Ajax 安装与卸载方法
Ajax Throws Sys.WebForms.PageRequestManagerErrorException with Response.Redirect的解决方法
asp.net 两个不同页面的传值
C# 可空类型分析

ASP.NET 中的 asp.net SqlDataAdapter对象使用札记


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 50 ::
收藏到网摘: n/a

如果 DataAdapter 遇到多个结果集,它将在 DataSet 中创建多个表。将向这些表提供递增的默认名称 TableN,以表示 Table0 的“Table”为第一个表名。 SqlDataAdapter
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
SqlCommand selectCMD = new SqlCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn);
selectCMD.CommandTimeout = 30;
SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;//通过SqlCommand给SqlDataAdapter设定参数,也可//直接用select语句
nwindConn.Open();
DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");
nwindConn.Close();
多个结果集
如果 DataAdapter 遇到多个结果集,它将在 DataSet 中创建多个表。将向这些表提供递增的默认名称 TableN,以表示 Table0 的“Table”为第一个表名。如果以参数形式向 Fill 方法传递表名称,则将向这些表提供递增的默认名称 TableNameN,这些表名称以表示 TableName0 的“TableName”为起始。
从多个 DataAdapter 填充 DataSet
可以将任意数量的 DataAdapter 与一个 DataSet 一起使用。每个 DataAdapter 都可用于填充一个或多个 DataTable 对象并将更新解析回相关数据源。DataRelation 和 Constraint 对象可以在本地添加到 DataSet,这样,您就可以使来自多个不同数据源的数据相关联。例如,DataSet 可以包含来自 Microsoft SQL Server 数据库、通过 OLE DB 公开的 IBM DB2 数据库以及对 XML 进行流处理的数据源的数据。一个或多个 DataAdapter 对象可以处理与每个数据源的通信。
以下代码示例从 Microsoft SQL Server 2000 上的 Northwind 数据库填充客户列表,从存储在 Microsoft? Access 2000 中的 Northwind 数据库填充订单列表。已填充的表通过 DataRelation 相关联,然后客户列表将与相应客户的订单一起显示出来。有关 DataRelation 对象的更多信息,请参见添加表间关系和导航表间关系。
SqlConnection custConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind;");
SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers", custConn);
OleDbConnection orderConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:\\Program Files\\Microsoft Office\\Office\\Samples\\northwind.mdb;");
OleDbDataAdapter orderDA = new OleDbDataAdapter("SELECT * FROM Orders", orderConn);
custConn.Open();
orderConn.Open();
DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");
orderDA.Fill(custDS, "Orders");
custConn.Close();
orderConn.Close();
DataRelation custOrderRel =
custDS.Relations.Add("CustOrders",custDS.Tables["Customers"].Columns["CustomerID"], custDS.Tables["Orders"].Columns["CustomerID"]);
foreach (DataRow pRow in custDS.Tables["Customers"].Rows)
{
Console.WriteLine(pRow["CustomerID"]);
foreach (DataRow cRow in pRow.GetChildRows(custOrderRel))
Console.WriteLine("\t" + cRow["OrderID"]);
}
SQL Server Decimal 类型
DataSet 使用 .NET Framework 数据类型来存储数据。对于大多数应用程序,这些类型都提供了一种方便的数据源信息表示形式。但是,当数据源中的数据类型是 SQL Server decimal 时,这种表示形式可能会导致问题。.NET Framework decimal 数据类型最多允许 28 个有效位,而 SQL Server decimal 数据类型则允许 38 个有效位。如果 SqlDataAdapter 在 Fill 操作过程中确定 SQL Server decimal 字段的精度大于 28 个字符,则当前行将不会被添加到 DataTable 中。此时将发生 FillError 事件,它使您能够确定是否将发生精度损失并作出适当的响应。有关 FillError 事件的更多信息,请参见使用 DataAdapter 事件。若要获取 SQL Server decimal 值,还可以使用 SqlDataReader 对象并调用 GetSqlDecimal 方法。
在 Update 过程中使用 SqlCommand,更改DataSet记录
以下示例使用派生类 OleDbDataAdapter 来对数据源进行 Update。此示例假定您已经创建了一个 OleDbDataAdapter 和一个 DataSet。
以下示例使用派生类 OleDbDataAdapter 来对数据源进行 Update。此示例假定您已经创建了一个 OleDbDataAdapter 和一个 DataSet。
public DataSet CreateCmdsAndUpdate(DataSet myDataSet,string myConnection,string mySelectQuery,string myTableName)
{
OleDbConnection myConn = new OleDbConnection(myConnection);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
myDataAdapter.SelectCommand = new OleDbCommand(mySelectQuery, myConn);
OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter);
myConn.Open();
DataSet custDS = new DataSet();
myDataAdapter.Fill(custDS);
//code to modify data in dataset here
myDataAdapter.Update(custDS, myTableName);
myConn.Close();
return custDS;
}
下面的实例将创建一个 SqlDataAdapter 并设置 SelectCommand 和 InsertCommand 属性。假定已经创建一个 SqlConnection 对象。
public static SqlDataAdapter CreateCustomerAdapter(SqlConnection conn)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd;
// Create the SelectCommand.
cmd = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", conn);
cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15);
da.SelectCommand = cmd;
// Create the InsertCommand.
cmd = new SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", conn);
cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
da.InsertCommand = cmd;
return da;
}
下面的实例创建一个 SqlDataAdapter 并设置 SelectCommand 和 DeleteCommand 属性。假定已经创建一个 SqlConnection 对象。
public static SqlDataAdapter CreateCustomerAdapter(SqlConnection conn)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd;
SqlParameter parm;
// Create the SelectCommand.
cmd = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", conn);
cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15);
da.SelectCommand = cmd;
// Create the DeleteCommand.
cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", conn);
parm = cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parm.SourceVersion = DataRowVersion.Original;
da.DeleteCommand = cmd;
return da;
}
下面的实例将创建一个 SqlDataAdapter 并设置 SelectCommand 和 UpdateCommand 属性。假定已经创建一个 SqlConnection 对象。
public static SqlDataAdapter CreateCustomerAdapter(SqlConnection conn)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd;
SqlParameter parm;
// Create the SelectCommand.
cmd = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", conn);
cmd.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
cmd.Parameters.Add("@City", SqlDbType.NVarChar, 15);
da.SelectCommand = cmd;
// Create the UpdateCommand.
cmd = new SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", conn);
cmd.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
parm = cmd.Parameters.Add("@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parm.SourceVersion = DataRowVersion.Original;
da.UpdateCommand = cmd;
return da;
}