The situation is following, i have a stored procedure in SQL Server , this has a few output parameters, one of them is a bit type, what I want is to take the value of that parameter, but I have a conversion error, InvalidCastException
.
情况如下,我在SQL Server中有一个存储过程,这里有一些输出参数,其中一个是位类型,我想要的是取该参数的值,但我有一个转换错误,InvalidCastException。
This is my code:
这是我的代码:
public void exec()
{
String strConnString = "Server=.\\SQLEXPRESS;Database=recalls;Integrated Security=true";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "findVinCamp";
int c = Int32.Parse(campaing.Text);
cmd.Parameters.Add("@camp", SqlDbType.Int).Value = c;
cmd.Parameters.Add("@vin", SqlDbType.VarChar, 100).Value = vin.Text;
cmd.Parameters.Add("@desc", SqlDbType.NVarChar, 255).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@st", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@bit", SqlDbType.Bit).Direction = ParameterDirection.Output;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
bit = (int)cmd.Parameters["@bit"].Value; //Exception Here
if (bit == 1)
{
desc.Text = cmd.Parameters["@desc"].Value.ToString();
stt.Text = cmd.Parameters["@st"].Value.ToString();
camp = cmd.Parameters["@camp"].Value.ToString();
if (stt.Text.Equals("APPLIED"))
{
stt.ForeColor = System.Drawing.Color.LawnGreen;
}
else
{
stt.ForeColor = System.Drawing.Color.Firebrick;
label3.Enabled = true;
newstatus.Enabled = true;
update.Enabled = true;
}
}
else
{
MessageBox.Show("Doesn't exits!");
}
}
I'm trying to assign the bit parameter to a int variable. Any question post on comments.
我正在尝试将bit参数分配给int变量。关于评论的任何问题。
4 个解决方案
#1
2
I change the (int)
to this, now works perfectly:
我将(int)更改为this,现在完美运行:
Boolean lol = Convert.ToBoolean(cmd.Parameters["@bit"].Value);
#2
1
Use this following Line
使用以下行
bool isConfirmed = (bool)cmd.Parameters["@bit"].Value;
if(isConfirmed ){
desc.Text = cmd.Parameters["@desc"].Value.ToString();
stt.Text = cmd.Parameters["@st"].Value.ToString();
camp = cmd.Parameters["@camp"].Value.ToString();
if (stt.Text.Equals("APPLIED"))
{
stt.ForeColor = System.Drawing.Color.LawnGreen;
}
else
{
stt.ForeColor = System.Drawing.Color.Firebrick;
label3.Enabled = true;
newstatus.Enabled = true;
update.Enabled = true;
}
}
else{
MessageBox.Show("Doesn't exits!");
}
**UPDATE: **if the bit column allows nulls -- many ways you can do this
**更新:**如果位列允许空值 - 您可以通过多种方式执行此操作
bool isConfirmed = cmd.Parameters["@bit"].Value as bool? ?? null;
and also read this- SQL Server Data Types and Their .NET Framework Equivalents
并阅读本文 - SQL Server数据类型及其.NET Framework等价物
#3
1
I believe a bit will convert to a boolean. Which should make your code a bit simpler too.
我相信有点会转换为布尔值。哪个应该使您的代码更简单一些。
i.e.
即
...
var bit = (bool)cmd.Parameters["@bit"].Value;
if (bit)
{
...
#4
0
You are trying to convert your boolean output to INT, please convert it in Boolean:
您正在尝试将布尔输出转换为INT,请将其转换为布尔值:
bool bitValue= Convert.ToBoolean(cmd.Parameters["@bit"].Value)
#1
2
I change the (int)
to this, now works perfectly:
我将(int)更改为this,现在完美运行:
Boolean lol = Convert.ToBoolean(cmd.Parameters["@bit"].Value);
#2
1
Use this following Line
使用以下行
bool isConfirmed = (bool)cmd.Parameters["@bit"].Value;
if(isConfirmed ){
desc.Text = cmd.Parameters["@desc"].Value.ToString();
stt.Text = cmd.Parameters["@st"].Value.ToString();
camp = cmd.Parameters["@camp"].Value.ToString();
if (stt.Text.Equals("APPLIED"))
{
stt.ForeColor = System.Drawing.Color.LawnGreen;
}
else
{
stt.ForeColor = System.Drawing.Color.Firebrick;
label3.Enabled = true;
newstatus.Enabled = true;
update.Enabled = true;
}
}
else{
MessageBox.Show("Doesn't exits!");
}
**UPDATE: **if the bit column allows nulls -- many ways you can do this
**更新:**如果位列允许空值 - 您可以通过多种方式执行此操作
bool isConfirmed = cmd.Parameters["@bit"].Value as bool? ?? null;
and also read this- SQL Server Data Types and Their .NET Framework Equivalents
并阅读本文 - SQL Server数据类型及其.NET Framework等价物
#3
1
I believe a bit will convert to a boolean. Which should make your code a bit simpler too.
我相信有点会转换为布尔值。哪个应该使您的代码更简单一些。
i.e.
即
...
var bit = (bool)cmd.Parameters["@bit"].Value;
if (bit)
{
...
#4
0
You are trying to convert your boolean output to INT, please convert it in Boolean:
您正在尝试将布尔输出转换为INT,请将其转换为布尔值:
bool bitValue= Convert.ToBoolean(cmd.Parameters["@bit"].Value)