2

我有一个 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 行。谢谢你。

4

2 回答 2

1

你确定它不缺少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 行包含在块中

于 2013-09-05T19:08:48.987 回答
1

我认为问题可能出在线路上while (readData.Read())。尝试将 { } 放在要设置值的代码行周围,看看这是否有帮助。

    while (readData.Read())
    {
        txtFName.Text = readData["FirstName"].ToString();
        txtLName.Text = readData["lastname"].ToString();
        ...
        lblPrice.Text = readData["Cost"].ToString();
    }
于 2013-09-05T19:08:16.013 回答