だから私はASP.Netでデータベースを使用して練習しています...
Northwindデータベースから2つのデータテーブル列があります。1つはでProduct.ProductID
、もう1つはですProduct.UnitsInStock
。それをオブジェクトのコレクション(SortedList)と比較する必要があります。基本的に変数の製品IDと数量(クライアント側)を比較します。オブジェクトは、にICollection
保存された値Session["cart"]
です。
製品IDと在庫単位の変数をDataTable列1がProducts.ProductIDで、2番目の列がProducts.UnitsInStockと比較するにはどうすればよいですか?
これがクエリであり、データベースからこれらの変数を取得できると私が考えたおおよその方法です。
DataTable dt = new DataTable();
if (dt.Columns.Count != 0 &&
dt.Rows.Count != 0)
{
int quantityOfUnit = 0;
int productID = 0;
for (int index = 0; index < dt.Columns.Count; index++)
{
if (index == indexOfUnitsInStock)//indexOfUnitsInStock = 1
{
quantityOfUnit = int.Parse(dt.Rows[0][index].ToString());
}
else//index = 0
{
productID = int.Parse(dt.Rows[0][index].ToString());
}
}
新しいクエリの作成:
foreach (object items in ((ShoppingCart)Session["cart"]).Values)
{
OleDbConnection conn = new OleDbConnection((string)Application["DBConnectionString"]);
string selectionString =
"SELECT Products.ProductID, Products.UnitsInStock " +
"FROM Products" +
"WHERE Products.ProductID = " +
((ShoppingCart)Session["cart"]).Values;
DataTable dt = new DataTable();
try
{
OleDbCommand cm = new OleDbCommand(selectionString, conn);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cm;
da.Fill(dt);
da.Dispose();
}
catch(Exception ex)
{
txtUnderstockedItems.Text = "There was a problem connecting to the database: " + ex.Message ;
}
finally
{
conn.Close();
}
}