Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# sql commnad不适用于c中的select语句_C#_.net_Windows Applications_Sqlcommand - Fatal编程技术网

C# sql commnad不适用于c中的select语句

C# sql commnad不适用于c中的select语句,c#,.net,windows-applications,sqlcommand,C#,.net,Windows Applications,Sqlcommand,我尝试使用sql命令执行以下代码,以获取输出并将其存储在整数变量中。对于空值插入,代码返回-1,这很好 但是,当数据库表中有值并且提供了正确的输入时,代码再次返回相同的-1值 有人能给我指出正确的方向吗 try { con.Open(); SqlCommand cmd1 = new SqlCommand(@"(Select ERSConversionFactorID FROM " + schemaName + "[ERSConversionFactors] WHERE [ERSC

我尝试使用sql命令执行以下代码,以获取输出并将其存储在整数变量中。对于空值插入,代码返回-1,这很好

但是,当数据库表中有值并且提供了正确的输入时,代码再次返回相同的-1值

有人能给我指出正确的方向吗

try {
    con.Open();
    SqlCommand cmd1 = new SqlCommand(@"(Select ERSConversionFactorID FROM " + schemaName + "[ERSConversionFactors] WHERE [ERSConversionFactor_CF] = @conversionvalue AND [ERSConversionFactor_Desc] = @convDescription)", con);

    if (comboBox_ConfacValue.Text == "")
    {
        cmd1.Parameters.Add("@conversionvalue", SqlDbType.NVarChar, 160).Value = DBNull.Value;
    }
    else
    {
        cmd1.Parameters.Add("@conversionvalue", SqlDbType.NVarChar, 160).Value = comboBox_ConfacValue.Text;
    }

    if (combobox_conversionDescription.Text == "")
    {
        cmd1.Parameters.Add("@convDescription", SqlDbType.NVarChar, 160).Value = DBNull.Value;
    }
    else
    {
        cmd1.Parameters.Add("@convDescription", SqlDbType.NVarChar, 160).Value = combobox_conversionDescription.Text;
    }

    string sql = "Select ERSConversionFactorID FROM " + schemaName + "[ERSConversionFactors] WHERE [ERSConversionFactor_CF] = @conversionvalue AND      [ERSConversionFactor_Desc] = @convDescription)";

    int conversionvalue = cmd1.ExecuteNonQuery();
}
catch (Exception ex)
{
    MessageBox.Show("Error : " + ex.Message);
}
finally
{
    con.Close();
}
谢谢

ExecuteOnQuery不用于从查询返回值。它执行查询,但只返回受INSERT、UPDATE或DELETE语句影响的行数

如果您查看MSDN第页的备注部分,您将发现返回值为-1的原因

如果希望SELECT语句检索第一行的第一列,则可以使用SELECT命令使用ExecuteReader或更好的ExecuteScalar。 但是,由于查询的WHERE语句可能导致无法检索任何行,因此应该在

ExecuteOnQuery不用于从查询返回值。它执行查询,但只返回受INSERT、UPDATE或DELETE语句影响的行数

如果您查看MSDN第页的备注部分,您将发现返回值为-1的原因

如果希望SELECT语句检索第一行的第一列,则可以使用SELECT命令使用ExecuteReader或更好的ExecuteScalar。 但是,由于查询的WHERE语句可能导致无法检索任何行,因此应该在

试试ExecuteScalar

int conversionvalue = cmd1.ExecuteScalar();
试试ExecuteScalar

int conversionvalue = cmd1.ExecuteScalar();

对于单个值,您需要使用ExecuteReader或ExecuteScalar。在本例中,我将使用ExecuteReader,因为似乎没有一个Gaurente始终返回一行

int? conversionvalue = null; // this will stay null if there is nothing read back
using(var reader = cmd1.ExecuteReader()) { // place the use of the reader in a using block to ensure it is cleaned up
    if(reader.Read()) // reader will return true if a record can be read. if you have multiple records you can turn the if into an while loop
        conversionvalue = reader.GetInt32(0); // read the value at ordinal position 0 as an int32
}

对于单个值,您需要使用ExecuteReader或ExecuteScalar。在本例中,我将使用ExecuteReader,因为似乎没有一个Gaurente始终返回一行

int? conversionvalue = null; // this will stay null if there is nothing read back
using(var reader = cmd1.ExecuteReader()) { // place the use of the reader in a using block to ensure it is cleaned up
    if(reader.Read()) // reader will return true if a record can be read. if you have multiple records you can turn the if into an while loop
        conversionvalue = reader.GetInt32(0); // read the value at ordinal position 0 as an int32
}

你需要研究这个-你需要研究这个-非常感谢它提供的信息。。。我会把这个标记为答案:非常感谢它提供的信息。。。我会将此标记为答案:Internal ExecuteScalar确实使用ExecuteReader,但请看一下关于甚至@Steve-谢谢的备注部分,我知道。我确实提到ExecuteScalar对于单个值是可以的,但是我的示例包括ExecuteReader,因为select中的语法。如果是选择前1名。。。或者选择COUNT或其他表示始终返回正好1个值或null的内容,我也会用ExecuteScalar的示例回答,或者根本不回答,因为您和techspider已经有两个很好的答案了。的确,内部ExecuteScalar使用ExecuteReader,但是请看一下关于甚至@Steve-谢谢的评论部分,我知道。我确实提到ExecuteScalar对于单个值是可以的,但是我的示例包括ExecuteReader,因为select中的语法。如果是选择前1名。。。或者选择COUNT或其他表示始终返回正好1个值或null的内容,我也会用ExecuteScalar的示例来回答,或者根本不回答,因为考虑到您和techspider已经有两个很好的答案。