我在这里遇到麻烦。我在windows_loaded事件中添加了它。代码在这里:
con.Open();
int i = 0, j = 0;
string[] productCode = null;
string[] productName = null;
int[] quantity =null;
float[] totalPrice = null;
float[] totalTax = null;
int orderID = 0;
SqlCeCommand com5 = new SqlCeCommand("SELECT MAX(OrderID) AS Expr1 FROM Order_Details", con);
orderID = (int)com5.ExecuteScalar();
SqlCeCommand com1 = new SqlCeCommand("SELECT ProductCode, Quantity FROM Order_Products WHERE OrderID = ('"+ orderID.ToString() +"')", con);
SqlCeDataReader dr1 = com1.ExecuteReader();
while (dr1.Read())
{
productCode[i] = (string)dr1[0]; // Exception is here
quantity[i] = (int)dr1[1];
SqlCeCommand com3 = new SqlCeCommand("SELECT ProductName FROM Products_Master WHERE ProductCode = '" + productCode[i] + "'", con);
productName[i] = (string)com3.ExecuteScalar();
i++;
}
SqlCeCommand com2 = new SqlCeCommand("SELECT TotalPrice, TotalTax FROM Order_Details WHERE OrderID = (SELECT MAX(OrderID) AS Expr1 FROM Order_Details)", con);
SqlCeDataReader dr2 = com1.ExecuteReader();
while (dr2.Read())
{
totalPrice[j] = (float)dr2[0];
totalTax[j] = (float)dr2[1];
j++;
}
for (int k = 1; k <= i; k++)
{
List<Product> Products = new List<Product>();
Product p = new Product
{
ID = k,
ProductName = productName[k],
Quantity = quantity[k],
Tax = totalTax[k],
Total = totalPrice[k]
};
dgrdInvoice.Items.Add(p); // add a row
}
con.Close();
任何人都可以找到解决此问题的方法吗?我试图将两个数据库中的值添加到单个DataGrid。
修改
实际上SQL查询中有一些东西:
数据类型对布尔表达式无效。 [数据类型(如果已知)= int,数据类型(如果未知)= nvarchar]。
答案 0 :(得分:1)
你有
string[] productCode = null;
并且永远不会将任何实数组分配给该变量。你的其他“数组”也是如此。
请注意,一旦创建,您就无法更改数组的长度。您可能需要查看List<T>
类。
答案 1 :(得分:0)
你应该声明productName []数组长度意味着必须初始化数组长度。
答案 2 :(得分:0)
我想说你正在将OrderID转换为字符串,而在你的数据库中,OrderID是一个整数。
你的查询不是很好......
orderID = (int)com5.ExecuteScalar();
SqlCeCommand com1 = new SqlCeCommand("SELECT ProductCode, Quantity FROM Order_Products WHERE OrderID = "+ orderID+")", con);
或与此非常相似的东西......
答案 3 :(得分:0)
在使用变量之前,应该对它们进行实例化。你不能这样做:
string[] productCode = null;
productCode[0] = "KD48Y";
//This results in: 'Object reference not set to an instance of an object.'
这将有效:
string[] productCode = new string[3]; //can contain 3 strings
productCode[0] = "KD48Y";
但在您的情况下,我建议您使用List<T>
,否则在达到初始限制时无法增加阵列的大小。
此:
SqlCeCommand com5 = new SqlCeCommand("SELECT MAX(OrderID) AS Expr1 FROM Order_Details", con);
orderID = (int)com5.ExecuteScalar();
SqlCeCommand com1 = new SqlCeCommand("SELECT ProductCode, Quantity FROM Order_Products WHERE OrderID = ('"+ orderID.ToString() +"')", con);
SqlCeDataReader dr1 = com1.ExecuteReader();
可以替换为:
SqlCeCommand com1 = new SqlCeCommand("SELECT ProductCode, Quantity FROM Order_Products
WHERE OrderID = (SELECT MAX(OrderID) AS Expr1
FROM Order_Details)", con);
要从datareader-object获取值,我建议您使用正确的方法,例如GetString()
或GetInt32()
。有关此问题的更多信息:SqlDataReader Methods。
在与数据源(如SQL数据库或其他导致异常的操作)进行交互时,最好使用异常处理。
SqlConnection connection = new SqlConnection("...");
SqlCommand sql = new SqlCommand("...");
List<Product> products = new List<Product>();
//other instantiations
try
{
connection.Open();
sql.ExecuteScalar();
//other operations
}
catch (Ecxeption ex)
{
//handle exception and show a message to the user
MessageBox.Show(ex.Message);
}
finally
{
//this will always execute, even when an exception occurs
//good for closing used resources
connection.Close();
}
我希望这能让你顺利上路! ;)