我想執行下面的代碼使用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();
}
感謝
你需要學習這一點 - https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.110%29.aspx?f= 255&MSPPError = -2147217396 – techspider