我有一个 winform,我正在尝试将数据保存到 SQL Server CE 数据库并检索该数据。
在我的 winfom 中运行以下命令时,我无法检索任何数据:
string lookupValue;
SqlCeConnection connection;
connection = new SqlCeConnection();
connection.ConnectionString = "Data Source= LaZSolutions.sdf";
SqlCeCommand cmd2 = connection.CreateCommand();
if (txtOrderNum.Text != null)
{
lookupValue = txtOrderNum.Text;
cmd2.CommandText = "SELECT Customers.FirstName, Customers.LastName, Customers.Address, Customers.City, Customers.State, Customers.Zip, Customers.Phone, Customers.Mobile, Customers.OrderNum, Customers.Comments, Customers.Email, Orders.OrderDate, Orders.OrderNum AS Expr1, Orders.Cost, Orders.Image FROM Customers INNER JOIN Orders ON Customers.OrderNum = Orders.OrderNum WHERE (Orders.OrderNum = '" + lookupValue + "')";
}
else if (txtFName.Text != null && txtLName.Text != null)
{
cmd2.CommandText = "SELECT Customers.*, Orders.* From Customers INNER JOIN Orders ON Customers.OrderNum = Orders.OrderNum where Customers.Firstname = '" + Convert.ToString(txtFName.Text) + "' and Customers.LastName = '" + Convert.ToString(txtLName.Text) + "'";
}
connection.Open();
cmd2.ExecuteNonQuery();
SqlCeDataReader readData = cmd2.ExecuteReader();
if (readData != null)
{
while (readData.Read())
txtFName.Text = readData["FirstName"].ToString();
txtLName.Text = readData["lastname"].ToString();
txtAddress.Text = readData["address"].ToString();
txtCity.Text = readData["city"].ToString();
txtState.Text = readData["state"].ToString();
txtZip.Text = readData["zip"].ToString();
txtHPhone.Text = readData["phone"].ToString();
txtMPhone.Text = readData["mobile"].ToString();
txtComments.Text = readData["Comments"].ToString();
txtEmail.Text = readData["email"].ToString();
txtOrderNum.Text = readData["OrderNum"].ToString();
txtOrderdate.Text = readData["orderdate"].ToString();
lblPrice.Text = readData["Cost"].ToString();
}
else
{
MessageBox.Show("No Results Found");
}
connection.Close();
有人能告诉我我的错误吗?当我在 VS 中运行时,查询返回 1 行。谢谢你。