需要帮助从 SQL Server CE 数据库中获取数据
本文关键字:数据库 获取 数据 CE Server 帮助 SQL | 更新日期: 2023-09-27 18:30:16
我有一个通知,我正在尝试将数据保存到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 行。谢谢。
我认为问题可能出在行while (readData.Read())
上。尝试在设置值的代码行周围放置 { },看看是否有帮助。
while (readData.Read())
{
txtFName.Text = readData["FirstName"].ToString();
txtLName.Text = readData["lastname"].ToString();
...
lblPrice.Text = readData["Cost"].ToString();
}
你确定它不缺少while循环的大括号
试试这个
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();
}
在 C# 中,如果省略循环或条件语句上的大括号,则块仅包含 NEXT 行