asp.net SqlDataAdapter對象使用劄記

來源:互聯網
上載者:User

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;
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.