需要帮助从SQL Server CE数据库中获取数据

时间:2013-09-05 19:02:55

标签: c# sql sql-server-ce

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

2 个答案:

答案 0 :(得分:1)

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

    while (readData.Read())
    {
        txtFName.Text = readData["FirstName"].ToString();
        txtLName.Text = readData["lastname"].ToString();
        ...
        lblPrice.Text = readData["Cost"].ToString();
    }

答案 1 :(得分: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行