2016-04-27 22 views
1

我想執行下面的代碼使用sql命令來獲取輸出並將其存儲在一個整數變量。代碼返回-1爲空值插入,這是很好的。sql commnad不工作在c中的select語句#

但是,當數據庫表中存在值並且給出正確的輸入時,代碼將再次返回相同的-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(); 
} 

感謝

+0

你需要學習這一點 - https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.110%29.aspx?f= 255&MSPPError = -2147217396 – techspider

回答

4

ExecuteNonQuery不打算被用來從查詢返回值。它執行查詢,但它只返回受INSERT,UPDATE或DELETE語句影響的行數。

如果您看一下MSDN上ExecuteNonQuery頁面上的REMARKS部分,您會發現返回值爲-1的原因。

使用SELECT命令,您可以使用ExecuteReader或更好的ExecuteScalar,如果您只是希望SELECT語句檢索到第一行的第一列。
然而,因爲你的查詢有一個WHERE語句,可能導致沒有檢索到的行,你應該對ExecuteScalar

object result = cmd1.ExecuteScalar(); 
if(result != null) 
{ 
    int conversionvalue = Convert.ToInt32(result); 
    ..... 

} 
+0

真棒感謝它的工作信息...我將這標記爲答案:) –

3

返回值增加檢查空嘗試的ExecuteScalar

int conversionvalue = cmd1.ExecuteScalar(); 
1

您需要對單個值使用ExecuteReader或ExecuteScalar。在這種情況下,我會使用一個ExecuteReader,因爲似乎沒有人會一直返回單個行。

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 
} 
+0

確實,內部ExecuteScalar使用ExecuteReader,但看看[MSDN上的ExecuteScalar頁面](https ://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.110%29.aspx)或甚至[在StackOverflow](http://stackoverflow.com/questions/2400005/does-execucalar-have-any-advantage-over-exec) – Steve

+1

@Steve - 謝謝,我知道。我提到ExecuteScalar對於單個值是可以的,但我的示例包含了ExecuteReader,因爲select中的語法。如果它是'SELECT TOP 1 ...'或'SELECT COUNT()'或其他表明總是返回1個值(或空值)的東西,那麼我也會以'ExecuteScalar'爲例(或者根本沒有回答,因爲已經有ExecuteScalar已經有兩個很好的答案(你的和techspider))。 – Igor