检索行标识

时间:2014-07-22 20:24:23

标签: c# sql sql-server

我想通过C#从SQL Server返回行ID。以下是我正在使用的代码:

const string sqlQuery = "SELECT ID " +
                        "FROM CleaningCycleTime " +
                        "WHERE ActualFinishDayTime < DATEADD(day, -60, GETDATE()) AND LotWorkOrder = @LotWorkOrder AND Process = @Process AND CleanType = @CleanType " +
                        "Group By ID " +
                        "Having (Min(ActualStartDayTime) IS NOT NULL AND Max(ActualFinishDayTime) IS NOT NULL)";

using (SqlCommand myCommand = new SqlCommand(sqlQuery, _myConnection))
{
    try
    {
        myCommand.Parameters.AddWithValue("@LotWorkOrder", lstOpenCleans.SelectedItem.ToString());
        myCommand.Parameters.AddWithValue("@Process", lstProcess.SelectedItem.ToString());
        myCommand.Parameters.AddWithValue("@CleanType", lstProcess.SelectedItem.ToString());
        _myConnection.Open();
        SqlDataReader myReader = myCommand.ExecuteReader();

        while (myReader.Read())
        {
            txtID.Text = myReader["ID"].ToString();
        }

        _myConnection.Close();
    }
    catch (Exception ee)
    {
        MessageBox.Show(ee.ToString());
        _myConnection.Close();
    }
}

生成

SELECT ID 
FROM CleaningCycleTime 
WHERE ActualFinishDayTime < DATEADD(day, -60, GETDATE()) 
  AND LotWorkOrder = 'X90744' 
  AND Process = 'PRE BLEND' 
  AND CleanType = 'FULL' 
GROUP BY 
   ID 
HAVING 
   (Min(ActualStartDayTime) IS NOT NULL 
   AND Max(ActualFinishDayTime) IS NOT NULL)

当我在SQL Server Management Studio中运行生成的查询时,它返回一个值。当我在C#中执行此操作时,它会达到myReader.Read(),但永远不会循环

1 个答案:

答案 0 :(得分:0)

我注意到了几件事:

1 - 在适当的情况下,您的C#字符串周围没有引号。     

SQL:

AND LotWorkOrder = 'X90744' 
AND Process = 'PRE BLEND' 
AND CleanType = 'FULL'

VS C#:

"AND LotWorkOrder = @LotWorkOrder AND Process = @Process AND CleanType = @CleanType "




2 - 我们看不到您的连接是如何定义的。为什么不将它包装在using语句中?

简明扼要:

using (var cn = new SqlConnection("connection string"))
{
    cn.Open();
    using (var cmd = cn.CreateCommand())
    {

        // set the command text
const string sqlQuery = "SELECT ID " +
                        "FROM CleaningCycleTime " +
                        "WHERE ActualFinishDayTime < DATEADD(day, -60, GETDATE()) AND LotWorkOrder = '@LotWorkOrder' AND Process = '@Process' AND CleanType = '@CleanType' " +
                        "Group By ID " +
                        "Having (Min(ActualStartDayTime) IS NOT NULL AND Max(ActualFinishDayTime) IS NOT NULL)";
        cmd.CommandText = sqlQuery;

        // Add your paramters to the command object.
        cmd.Parameters.AddWithValue("@LotWorkOrder", lstOpenCleans.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@Process", lstProcess.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@CleanType", lstProcess.SelectedItem.ToString());

        using (var reader = cmd.ExecuteReader())
        {

            while (reader.Read())
            {
                txtID.Text = myReader["ID"].ToString();
            }
        }
    }
}

新的sqlQuery:

const string sqlQuery = "SELECT ID " +
                        "FROM CleaningCycleTime " +
                        "WHERE ActualFinishDayTime < DATEADD(day, -60, GETDATE()) AND LotWorkOrder = '@LotWorkOrder' AND Process = '@Process' AND CleanType = '@CleanType' " +
                        "Group By ID " +
                        "Having (Min(ActualStartDayTime) IS NOT NULL AND Max(ActualFinishDayTime) IS NOT NULL)";