简体   繁体   English

ADO.NET将多个值插入SQL参数

[英]ADO.NET insert multiple values to SQL parameter

I am receiving a Dictionary<string, string> and would like to forward its values to the DB inside SqlParameter . 我正在接收Dictionary<string, string>并想将其值转发到SqlParameter的数据库。 Is that even possible? 那有可能吗? This is the way I did it, and I am getting an error that column name doesn't match table definition. 这是我这样做的方式,并且我收到一个错误,指出列名与表定义不匹配。

SqlParameter param = new SqlParameter();
param.ParameterName = "@Values";

var sb = new StringBuilder();
foreach (var item in data)
{
    sb.Append("'" + item.Value + "', ");
}
param.Value = sb.ToString().TrimEnd(',');

string insertString = $"insert into {tableName} values (@Values)";
SqlCommand command = new SqlCommand(insertString, connection);

command.Parameters.Add(param);
command.ExecuteNonQuery();

Sql server can't interpret the single variable you are passing as multiple values. SQL Server无法将您传递的单个变量解释为多个值。
You can either generate your query with multiple variables, or use a table valued parameter. 您可以使用多个变量生成查询,也可以使用表值参数。
For the first option, you must change the way you build your query: 对于第一种选择,您必须更改构建查询的方式:

var command = new SqlCommand();

var insertString = $"insert into {tableName} values (";
var sb = new StringBuilder(insertString);
int i = 0;
foreach (var item in data)
{
    sb.Append("@P").Append(i).Append(",");
    command.Parameters.Add("@P" + i, SqlDbType.VarChar).Value = item.Value;
    i++;
}
command.Connection = connection;
command.CommandText = sb.ToString().TrimEnd(",") + ");";
command.ExecuteNonQuery();

Note: Code was not tested, there might be some errors. 注意:代码未经测试,可能存在一些错误。

For the second option, You must use a stored procedure. 对于第二个选项,您必须使用存储过程。 I've never tried to pass table valued parameters to an inline query and I don't think it's possible. 我从未尝试过将表值参数传递给内联查询,而且我认为这是不可能的。
This post (also linked in Alex K's comment) explains how to do that. 这篇文章 (也链接到Alex K的评论中)解释了如何做到这一点。

Each value in the "Values" part of you t-SQL must be enclosed with parenthesis. t-SQL的“值”部分中的每个值都必须用括号括起来。

So, just change this line: 因此,只需更改此行:

    sb.Append("'" + item.Value + "', ");

to: 至:

    sb.Append("('" + item.Value + "'),");  // note: no space after the ,

Your tSQL would look something like this: 您的tSQL看起来像这样:

    insert into myTable values ('A', 'B', 'C',)

It needs to look like this (assuming you've only got 1 column in the table): 它需要看起来像这样(假设表中只有1列):

    insert into myTable values ('A'), ('B'), ('C')

And if your table contains multiple columns: 并且如果您的表包含多个列:

    insert into myTable (myColumn) values ('A'), ('B'), ('C')

I think the best is create a split function in mssql (million of example in internet)and a stored. 我认为最好的方法是在mssql中创建一个拆分函数(Internet中有数百万个示例)并存储一个。 Pass a string comma(for example) separated to the stored Who call the function. 将字符串逗号(例如)传递给存储的Who调用函数。 Sorry for no example but i'm with my smartphone 抱歉,没有例子,但我正在使用智能手机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM