C# 转换nvarchar值时转换失败。。到数据类型int
我试图执行以下查询,但得到一个异常C# 转换nvarchar值时转换失败。。到数据类型int,c#,sql,asp.net,sql-server,parameter-passing,C#,Sql,Asp.net,Sql Server,Parameter Passing,我试图执行以下查询,但得到一个异常 using (SqlConnection con = new SqlConnection(UserDatabase.getConnectionString())) { using (SqlCommand cmd = new SqlCommand("SELECT * FROM Order_Header where Status IN (@Values)")) { using (SqlDataAdapter sda = new Sq
using (SqlConnection con = new SqlConnection(UserDatabase.getConnectionString()))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Order_Header where Status IN (@Values)"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
try
{
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
// This is for test purposes
List<int> yourValues = new List<int>() { 1, 2, 3, 4, 5 };
//Get values for IN
string x = String.Join(",", yourValues.Select(s => String.Format("'{0}'", s)).ToArray());
// Add parameter
cmd.Parameters.AddWithValue("@Values", x);
DataTable dt = new DataTable();
sda.Fill(dt);
order_details.SetDataSource(dt);
SalesReport.ReportSource = order_details;
}
catch (Exception ex)
{
scriptMessage(ex.ToString);
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
}
}
执行此查询时,我得到以下异常:
将nvarchar值1'、'2'、'3'、'4'、'5转换为数据类型int时,转换失败
为什么会这样?请帮助您的状态为varchar,列表为int。 如果包含以下数据,则需要将状态强制转换为int:- 从选择“1”作为statusx中选择caststatus作为int 更新 我已将您的代码更改为以下代码,现在它工作正常: 方法1:
using (SqlConnection con =new SqlConnection(/*Your Connection String*/))
{
using (SqlCommand cmd = new SqlCommand())
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
try
{
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
// This is for test purposes
List<int> yourValues = new List<int>() { 1, 2, 3, 4, 5 };
//Get values for IN
string x = string.Join(",", yourValues.Select(s => String.Format("{0}", s)).ToArray());
cmd.CommandText = "SELECT* FROM Order_Header where Status IN(" + x + ")";
DataTable dt = new DataTable();
sda.Fill(dt);
}
catch
(Exception
ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
}
现在,您可以按如下方式编写代码:
using (SqlConnection con =new SqlConnection(/*Your Connection String*/))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Order_Header where Status IN (Select RowIndex from Split(@Values,','))"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
try
{
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
// This is for test purposes
List<int> yourValues = new List<int>() { 1, 2, 3, 4, 5 };
//Get values for IN
string x = String.Join(",", yourValues.Select(s => String.Format("{0}", s)).ToArray());
// Add parameter
cmd.Parameters.AddWithValue("@Values", x);
DataTable dt = new DataTable();
sda.Fill(dt);
//order_details.SetDataSource(dt);
//SalesReport.ReportSource = order_details;
}
catch
(Exception
ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
}
现在这也可以正常工作了。String.Format{0},s。无论如何,您应该将多个参数作为TVP传递,而不是构建自定义参数sql@lad2025我也试过了。我也有同样的例外。除非@parameter是表值参数,否则不能在@parameter中执行此操作。您正在传递一个值,一个字符串,包含单个字符串值“1”、“2”、“3”、“4”、“5”。这行不通。@LasseV.Karlsen还有其他方法吗?复制效果很差,因为它会生成一个表扫描。有更好的方法,使用表值参数或生成正确的SQL。我现在找不到一个好的副本来链接,其他人可能会更幸运。状态为int,我尝试更改以下语句:string x=string.Join,,yourValues.Selects=>string.Format{0},s.ToArray@用户1377504或尝试使用CAST@Values正如INT所示,这是一个值字符串,而不是单个值。我不确定是否像INT那样施法work@user1377504但是你可以尝试这样做,理想情况下,查询应该是从Order_头中选择*,其中状态为'1'、'2'、'3'、'4'、'5',但状态为int,所以你需要将这些数字列表转换为int,以使查询正常工作。我已经尝试过了,但它给了我同样的例外。
using (SqlConnection con =new SqlConnection(/*Your Connection String*/))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Order_Header where Status IN (Select RowIndex from Split(@Values,','))"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
try
{
cmd.Connection = con;
con.Open();
sda.SelectCommand = cmd;
// This is for test purposes
List<int> yourValues = new List<int>() { 1, 2, 3, 4, 5 };
//Get values for IN
string x = String.Join(",", yourValues.Select(s => String.Format("{0}", s)).ToArray());
// Add parameter
cmd.Parameters.AddWithValue("@Values", x);
DataTable dt = new DataTable();
sda.Fill(dt);
//order_details.SetDataSource(dt);
//SalesReport.ReportSource = order_details;
}
catch
(Exception
ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
}