色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

asp.net SqlDataAdapter對(duì)象使用札記

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設(shè)定參數(shù),也可//直接用select語句
nwindConn.Open();
DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");
nwindConn.Close();
多個(gè)結(jié)果集
如果 DataAdapter 遇到多個(gè)結(jié)果集,它將在 DataSet 中創(chuàng)建多個(gè)表。將向這些表提供遞增的默認(rèn)名稱 TableN,以表示 Table0 的“Table”為第一個(gè)表名。如果以參數(shù)形式向 Fill 方法傳遞表名稱,則將向這些表提供遞增的默認(rèn)名稱 TableNameN,這些表名稱以表示 TableName0 的“TableName”為起始。
從多個(gè) DataAdapter 填充 DataSet
可以將任意數(shù)量的 DataAdapter 與一個(gè) DataSet 一起使用。每個(gè) DataAdapter 都可用于填充一個(gè)或多個(gè) DataTable 對(duì)象并將更新解析回相關(guān)數(shù)據(jù)源。DataRelation 和 Constraint 對(duì)象可以在本地添加到 DataSet,這樣,您就可以使來自多個(gè)不同數(shù)據(jù)源的數(shù)據(jù)相關(guān)聯(lián)。例如,DataSet 可以包含來自 Microsoft SQL Server 數(shù)據(jù)庫、通過 OLE DB 公開的 IBM DB2 數(shù)據(jù)庫以及對(duì) XML 進(jìn)行流處理的數(shù)據(jù)源的數(shù)據(jù)。一個(gè)或多個(gè) DataAdapter 對(duì)象可以處理與每個(gè)數(shù)據(jù)源的通信。
以下代碼示例從 Microsoft SQL Server 2000 上的 Northwind 數(shù)據(jù)庫填充客戶列表,從存儲(chǔ)在 Microsoft? Access 2000 中的 Northwind 數(shù)據(jù)庫填充訂單列表。已填充的表通過 DataRelation 相關(guān)聯(lián),然后客戶列表將與相應(yīng)客戶的訂單一起顯示出來。有關(guān) DataRelation 對(duì)象的更多信息,請(qǐng)參見添加表間關(guān)系和導(dǎo)航表間關(guān)系。
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 數(shù)據(jù)類型來存儲(chǔ)數(shù)據(jù)。對(duì)于大多數(shù)應(yīng)用程序,這些類型都提供了一種方便的數(shù)據(jù)源信息表示形式。但是,當(dāng)數(shù)據(jù)源中的數(shù)據(jù)類型是 SQL Server decimal 時(shí),這種表示形式可能會(huì)導(dǎo)致問題。.NET Framework decimal 數(shù)據(jù)類型最多允許 28 個(gè)有效位,而 SQL Server decimal 數(shù)據(jù)類型則允許 38 個(gè)有效位。如果 SqlDataAdapter 在 Fill 操作過程中確定 SQL Server decimal 字段的精度大于 28 個(gè)字符,則當(dāng)前行將不會(huì)被添加到 DataTable 中。此時(shí)將發(fā)生 FillError 事件,它使您能夠確定是否將發(fā)生精度損失并作出適當(dāng)?shù)捻憫?yīng)。有關(guān) FillError 事件的更多信息,請(qǐng)參見使用 DataAdapter 事件。若要獲取 SQL Server decimal 值,還可以使用 SqlDataReader 對(duì)象并調(diào)用 GetSqlDecimal 方法。
在 Update 過程中使用 SqlCommand,更改DataSet記錄
以下示例使用派生類 OleDbDataAdapter 來對(duì)數(shù)據(jù)源進(jìn)行 Update。此示例假定您已經(jīng)創(chuàng)建了一個(gè) OleDbDataAdapter 和一個(gè) DataSet。
以下示例使用派生類 OleDbDataAdapter 來對(duì)數(shù)據(jù)源進(jìn)行 Update。此示例假定您已經(jīng)創(chuàng)建了一個(gè) OleDbDataAdapter 和一個(gè) 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;
}
下面的實(shí)例將創(chuàng)建一個(gè) SqlDataAdapter 并設(shè)置 SelectCommand 和 InsertCommand 屬性。假定已經(jīng)創(chuàng)建一個(gè) SqlConnection 對(duì)象。
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;
}
下面的實(shí)例創(chuàng)建一個(gè) SqlDataAdapter 并設(shè)置 SelectCommand 和 DeleteCommand 屬性。假定已經(jīng)創(chuàng)建一個(gè) SqlConnection 對(duì)象。
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;
}
下面的實(shí)例將創(chuàng)建一個(gè) SqlDataAdapter 并設(shè)置 SelectCommand 和 UpdateCommand 屬性。假定已經(jīng)創(chuàng)建一個(gè) SqlConnection 對(duì)象。
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;
}

AspNet技術(shù)asp.net SqlDataAdapter對(duì)象使用札記,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 邢台市| 宣化县| 安福县| 南丰县| 东丰县| 揭东县| 措美县| 五台县| 瓮安县| 中山市| 收藏| 集贤县| 曲靖市| 乳山市| 收藏| 新巴尔虎左旗| 思茅市| 寿宁县| 十堰市| 咸阳市| 武乡县| 安图县| 五常市| 红桥区| 三亚市| 上林县| 宿松县| 巴马| 乌拉特中旗| 永春县| 敖汉旗| 远安县| 黑龙江省| 江安县| 江都市| 婺源县| 裕民县| 耒阳市| 巧家县| 拉孜县| 娄底市|