从C#Winforms将多个值插入SQL Server列

时间:2022-12-03 09:37:45

I have a table for translators, and each translator is capable of translating from one source language to many target languages. I am creating a Windows form application for such a table, and have tried using a listbox for such an entry, but it gives me the following error

我有一个翻译表,每个翻译都能够从一种源语言翻译成许多目标语言。我正在为这样的表创建一个Windows表单应用程序,并尝试使用列表框进行此类条目,但它给了我以下错误

The variable name '@lang_code' has already been declared. Variable names must be unique within a query batch or stored procedure.

变量名'@lang_code'已经被声明。变量名在查询批处理或存储过程中必须是唯一的。

Code:

List<String> SelectedItems=new List<String>();

foreach (System.Data.DataRowView s in listBox1.SelectedItems)
{           
    string select = s.ToString();
    SelectedItems.Add(select);
    myCmd.Parameters.AddWithValue("@lang_code", select);
}

2 个解决方案

#1


1  

Based on the error, looks like you have added the @lang_code parameter to myCmd (SqlCommand) and have not cleared that parameter collection. So the SqlCommand already has a parameter called @lang_code. You could create multiple commands within one SqlConnection, or clear the parameters collection prior to re-adding @lang_code

基于该错误,看起来您已将@lang_code参数添加到myCmd(SqlCommand)并且尚未清除该参数集合。所以SqlCommand已经有一个名为@lang_code的参数。您可以在一个SqlConnection中创建多个命令,或在重新添加@lang_code之前清除参数集合

#2


0  

You are adding a parameter named "@lang_code" multiple times to the same query. This isn't legal in any SQL language I'm aware of. This would be the equivalent of declaring multiple c# variables with the same name in the same scope.

您正在多次向同一查询添加名为“@lang_code”的参数。这在我所知道的任何SQL语言中都是不合法的。这相当于在同一范围内声明具有相同名称的多个c#变量。

It sounds to me like you actually want 3 separate tables with relationships between them.

听起来像你真的想要3个独立的表,它们之间有关系。

A table of Languages, a table with Translators, and a table containing the languages that each Translator knows. The Languages table is just a list of all the languages that your application is aware of. The Translator table should have a private key, with one row per translator. The LanguagesKnown table should have one row per translator per language known, with a foreign key pointing to the primary key of the translator and the primary key of the language.

语言表,带翻译器的表格,以及包含每个译员都知道的语言的表格。 Languages表只是应用程序知道的所有语言的列表。转换器表应该有一个私钥,每个转换器有一行。每个已知语言的每个翻译器应该有一行LanguagesKnown表,外键指向翻译器的主键和语言的主键。

You will end up with a very shallow tree structure that a relational database can easily handle. It will look something like this:

最终会得到一个非常浅的树结构,关系数据库可以轻松处理。它看起来像这样:

Language

  1. LanguageId
  2. DisplayName

Translator

  1. TranslatorId (pk)
  2. Name

LanguageKnown

  1. LanguageKnownId
  2. TranslatorId
  3. LanguageId

#1


1  

Based on the error, looks like you have added the @lang_code parameter to myCmd (SqlCommand) and have not cleared that parameter collection. So the SqlCommand already has a parameter called @lang_code. You could create multiple commands within one SqlConnection, or clear the parameters collection prior to re-adding @lang_code

基于该错误,看起来您已将@lang_code参数添加到myCmd(SqlCommand)并且尚未清除该参数集合。所以SqlCommand已经有一个名为@lang_code的参数。您可以在一个SqlConnection中创建多个命令,或在重新添加@lang_code之前清除参数集合

#2


0  

You are adding a parameter named "@lang_code" multiple times to the same query. This isn't legal in any SQL language I'm aware of. This would be the equivalent of declaring multiple c# variables with the same name in the same scope.

您正在多次向同一查询添加名为“@lang_code”的参数。这在我所知道的任何SQL语言中都是不合法的。这相当于在同一范围内声明具有相同名称的多个c#变量。

It sounds to me like you actually want 3 separate tables with relationships between them.

听起来像你真的想要3个独立的表,它们之间有关系。

A table of Languages, a table with Translators, and a table containing the languages that each Translator knows. The Languages table is just a list of all the languages that your application is aware of. The Translator table should have a private key, with one row per translator. The LanguagesKnown table should have one row per translator per language known, with a foreign key pointing to the primary key of the translator and the primary key of the language.

语言表,带翻译器的表格,以及包含每个译员都知道的语言的表格。 Languages表只是应用程序知道的所有语言的列表。转换器表应该有一个私钥,每个转换器有一行。每个已知语言的每个翻译器应该有一行LanguagesKnown表,外键指向翻译器的主键和语言的主键。

You will end up with a very shallow tree structure that a relational database can easily handle. It will look something like this:

最终会得到一个非常浅的树结构,关系数据库可以轻松处理。它看起来像这样:

Language

  1. LanguageId
  2. DisplayName

Translator

  1. TranslatorId (pk)
  2. Name

LanguageKnown

  1. LanguageKnownId
  2. TranslatorId
  3. LanguageId