#副標=資料庫與網頁的連結
#大標= ASP.NET 2.0資料存取新面貌(2)
#眉標=ASP.NET
#作者=王寧疆
----------------BOX 程式1-----------------
using System; //引入必要的名稱空間
using System.Collections;
using System.Data; //引入ADO.NET必要的名稱空間
using System.Data.SqlClient;
public class Model //提供存取資料庫記錄功能的類別
{
//負責查詢供應商記錄的方法
public static DataSet GetProducts(String SupplierID)
{
//依據指定的供應商代號查詢對應的產品記錄的SQL敘述
string strSQL = String.Format(
"select * from Products where SupplierID = {0}",
SupplierID);
//建立SqlConnection類別的物件
SqlConnection conn =
new SqlConnection(
"server=(local);database=Northwind;User ID=sa");
//建立SqlDataAdapter類別的物件
SqlDataAdapter da =
new SqlDataAdapter(strSQL, conn);
DataSet ds = new DataSet();//建立DataSet類別的物件
//查詢產品記錄,並將查詢的結果放到DataSet類別的物件
da.Fill(ds);
return ds; //傳回查詢的結果
}
//負責更新供應商記錄的方法
public static void UpdateProducts(
int original_ProductId, string ProductName, int
SupplierID, int CategoryID, string
QuantityPerUnit, decimal UnitPrice,
Int16 UnitsInStock, Int16 UnitsOnOrder,
Int16 ReorderLevel,bool Discontinued)
{
//依據參數的內容更新資料庫中產品記錄的SQL敘述
string strSQL = String.Format(
"Update Products set ProductName='{0}', " +
"SupplierID={1}, CategoryID={2}, " +
"QuantityPerUnit='{3}', UnitPrice={4}, " +
"UnitsInStock={5}, UnitsOnOrder={6}, " +
"ReorderLevel={7}, Discontinued={8} " +
"where ProductID = {9}", ProductName,
SupplierID, CategoryID, QuantityPerUnit,
UnitPrice, UnitsInStock, UnitsOnOrder,
ReorderLevel, Discontinued?1:0,
original_ProductId);
SqlConnection conn =
new SqlConnection(
"server=.;database=Northwind;User ID=sa");
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = CommandType.Text;
conn.Open();
try
{
cmd.ExecuteNonQuery();//執行更新產品記錄的動作
}
catch (Exception ex)
{
throw (ex); //將例外擲回給呼叫者
}
finally
{
cmd.Dispose(); //丟棄SqlCommand類別的物件
conn.Close(); //關閉資料庫連線
conn.Dispose();//丟棄SqlConnection類別的物件
}
}
//負責刪除供應商記錄的方法
public static void DeleteProducts(int original_ProductId)
{
//依據參數的內容刪除資料庫中產品記錄的SQL敘述
string strSQL =
String.Format(
"Delete from Products where ProductID = {0}",
original_ProductId);
SqlConnection conn =
new SqlConnection(
"server=.;database=Northwind;User ID=sa");
SqlCommand cmd = new SqlCommand(strSQL, conn);
cmd.CommandType = CommandType.Text;
conn.Open(); //開啟資料庫連線
try
{
cmd.ExecuteNonQuery();//執行刪除產品記錄的動作
}
catch (Exception ex)
{
throw (ex); //將例外擲回給呼叫者
}
finally
{
cmd.Dispose(); //丟棄SqlCommand類別的物件
conn.Close(); //關閉資料庫連線
conn.Dispose(); //丟棄SqlConnection類別的物件
}
}
//負責查詢供應商記錄的方法
public static DataSet GetSuppliers()
{
string strSQL =
"select SupplierID, CompanyName from Suppliers";
SqlConnection conn =
new SqlConnection(
"server=(local);database=Northwind;User ID=sa");
SqlDataAdapter da =
new SqlDataAdapter(strSQL, conn);
DataSet ds = new DataSet(); //建立DataSet類別的物件
da.Fill(ds);
return ds; //傳回查詢的結果
}
}
-----------------END-----------------
-------------------------box 程式2--------------------------
-----------------------------end-----------------------
---------------------------------box 程式3--------------------------
-----------------------------end------------------------------
---------------------box 程式4--------------------
---------------------------end-------------------------