插入多个值 [英] insert multiple values

查看:61
本文介绍了插入多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在sql server 2005中有一个表,它的名称是employee,它是一列

名称

我在C#窗口应用程序中有一个文本框和一个按钮
文本框名称为txtid,按钮名称为btnsave
现在我在运行时输入的txtname是"john,salman",然后单击btnsave
然后自动将数据保存在sql server 2005中
当我输入逗号(,)时,会自动在sql server 2005中保存另一行,例如
名称
约翰
萨尔曼

那我该怎么办?

请帮助我

在此先谢谢您.

Hi
i have a one table in sql server 2005 and it''s name is employee and it''s one column
such as
name

i have a one textbox and one button in C# window application
textbox name is txtid and button name is btnsave
now i enter at runtime in txtname is ''john,salman'' and click a btnsave
then automatic save data in sql server 2005
when i enter a coma(,) then automatic save a another row in sql server 2005 such as
name
john
salman

so what i do you do?

please help me

thanks in advance

推荐答案

您可以将逗号分隔的字符串传递给数据库,并基于'拆分字符串,并将其插入到表中.

要将字符串拆分为表格,您可以创建一个函数,该函数接受字符串并将其以表格形式返回.

您可以在这里找到一个例子
http://stackoverflow. com/questions/314824/t-sql-opposite-to-string-concatenation-how-to-split-string-into-multiple-recor [ http://fluppe.wordpress.com/2005/12/27/sql- split-string-into-table [ ^ ]
You can pass the comma separated string to database, and split the string on the basis of ‘,’ and insert them to the table.

To split the string into table you can create a function which takes the string and returns it back in the form of table.

You can find one example here
http://stackoverflow.com/questions/314824/t-sql-opposite-to-string-concatenation-how-to-split-string-into-multiple-recor[^]

http://fluppe.wordpress.com/2005/12/27/sql-split-string-into-table[^]


SqlConnection con=new SqlConnection("server=servername;integrated security=true;database=databasename");
string[]Values=txtname.Text.Split(',');
SqlCommand cmd;
for(int i=0;i<values.length;i++)>
{
    cmd=new SqlCommand("Insert into employee(Name) Values(@Name)",con);
    cmd.Parameters.Add("@Name",SqlDbType.Varchar(30)).Value=Values[i];
    if(con.State==ConnectionState.Closed)
    {
        con.Open();
    }
    cmd.ExecuteNonQuery();
    cmd.Dispose();
}


问候
Sajid Ahmed Shahsroha


regards
Sajid Ahmed Shahsroha


这篇关于插入多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆