[英]OleDb parameters for SQL statement to Access
我一直在尋找和調試這段代碼,看不到任何錯誤。 目前的錯誤是No Value given for one or more required Parameters
唯一需要的列是prodName
,並且始終對其進行檢查以確保字符串不為空,否則可以作為整數和貨幣值的長度參數嗎? 我只是想讓它們盡可能大,以接受最合理大小的數據字段。
我還擔心我的布爾值不適合“為null,不為null”類別,但是如果未選中該復選框,它將返回false。
這是我的參數,如果你們簡短地查看一下我可能會犯的語法或邏輯錯誤,我將不勝感激,我在C#中處於中等水平
public int updateProducts(int prodId, string productName, string supplier, string category, string quantityPerUnit, string unitPrice, string unitsInStock, string unitsOnOrder, string reorderLevel, bool discontinued)
{
int rows = 0;
int PId = prodId;
string prodName = productName;
int supp = int.Parse(supplier);
int? categoryID = 0;
string qPerUnit = quantityPerUnit;
Decimal? uPrice = 0;
int? uInStock = 0;
int? uOnOrder = 0;
int? reorderLvl = 0;
bool disc = discontinued;
if (!string.IsNullOrWhiteSpace(unitPrice))
{
uPrice = Decimal.Parse(unitPrice);
}
if (!string.IsNullOrWhiteSpace(category))
{
categoryID = int.Parse(category);
}
if (!string.IsNullOrWhiteSpace(unitsInStock))
{
uInStock = int.Parse(unitsInStock);
}
if (!string.IsNullOrWhiteSpace(unitsOnOrder))
{
uOnOrder = int.Parse(unitsOnOrder);
}
if (!string.IsNullOrWhiteSpace(reorderLevel))
{
reorderLvl = int.Parse(reorderLevel);
}
string dbString = "UPDATE [Products] SET [ProductName]= ?,[Supplier]= ?,[Category]= ?,[QuantityPerUnit]= ?,[UnitPrice]= ?,[UnitsInStock]= ?,[UnitsOnOrder]= ?,[ReorderLevel]= ?, [Discontinued]= ? WHERE [SupplierID]= " + PId;
try
{
conn.Open();
oleCommand = new OleDbCommand(dbString, conn);
oleCommand.Parameters.Add(new OleDbParameter("@prodName", OleDbType.VarChar, 30, ParameterDirection.Input, false, 10, 0, "ProductName", DataRowVersion.Original, null)).Value = prodName;
oleCommand.Parameters.Add(new OleDbParameter("@supp", OleDbType.Integer, 40, ParameterDirection.Input, false, 10, 0, "Supplier", DataRowVersion.Original, null)).Value = supp;
oleCommand.Parameters.Add(new OleDbParameter("@cat", OleDbType.Integer, 30, ParameterDirection.Input, true, 10, 0, "Category", DataRowVersion.Original, null)).Value = categoryID.HasValue ? (object)categoryID : DBNull.Value;
oleCommand.Parameters.Add(new OleDbParameter("@qPU", OleDbType.VarChar, 20, ParameterDirection.Input, true, 10, 0, "QuantityPerUnit", DataRowVersion.Original, null)).Value = string.IsNullOrWhiteSpace(qPerUnit) ? DBNull.Value : (object)qPerUnit;
oleCommand.Parameters.Add(new OleDbParameter("@uPrice", OleDbType.Currency, 6, ParameterDirection.Input, true, 10, 0, "UnitPrice", DataRowVersion.Original, null)).Value = uPrice.HasValue ? (object)uPrice : DBNull.Value;
oleCommand.Parameters.Add(new OleDbParameter("@uInStock", OleDbType.Integer, 50, ParameterDirection.Input, true, 10, 0, "UnitsInStock", DataRowVersion.Original, null)).Value = uInStock.HasValue ? (object)uPrice : DBNull.Value;
oleCommand.Parameters.Add(new OleDbParameter("@uOnOrder", OleDbType.Integer, 50, ParameterDirection.Input, true, 10, 0, "UnitsOnOrder", DataRowVersion.Original, null)).Value = uOnOrder.HasValue ? (object)uOnOrder : DBNull.Value;
oleCommand.Parameters.Add(new OleDbParameter("@reorderLvl", OleDbType.Integer, 50, ParameterDirection.Input, true, 10, 10, "ReorderLevel", DataRowVersion.Original, null)).Value = reorderLvl.HasValue ? (object)reorderLvl : DBNull.Value;
oleCommand.Parameters.Add(new OleDbParameter("@discontinued", OleDbType.Boolean, 10, ParameterDirection.Input, true, 10, 0, "Discontinued", DataRowVersion.Original, null)).Value = disc;
rows = (int)oleCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
dbError = "Add Supplier command Error: " + ex.Message;
}
finally
{
conn.Close();
}
return rows;
}
是方括號。 方括號表示Jet Sql的參數。 但這也意味着“此標識符中有空格”
您的標識符都沒有空格,因此只需刪除方括號即可。 如果確實有帶空格的標識符,則必須完全限定該名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.