Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
string str = string.Empty;
ListItemCollection lst = lstuser1.Items;
foreach (ListItem assigned in lst)
{
   str = assigned.Text +" ,";
}
string query = "insert into UserInformation(new_name) values('" + str + "')";
SqlCommand cmd = new SqlCommand(query,c);
c.Open();
cmd.ExecuteNonQuery();
c.Close();

can anyone please tell me,i want to insert multiple items one time so how it is possible.
Posted
Updated 7-Mar-14 8:47am
v2
Comments
phil.o 7-Mar-14 14:50pm    
You did not specify which database system. With SQL Server CE, for example, multi inserts in one command are not supported at all.

The syntax of a multiple INSERT is not like that! It's:
SQL
INSERT INTO MyTable (MyColumnName) VALUES (FirstValue) (SecondValue) (ThirdValue)
But...it is a very poor idea to do it the way you are: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
In this case, you would be better off using a DataTable to create the bulk insert:
C#
string strConnect = @"Data Source=GRIFFPC\SQLEXPRESS;Initial Catalog=Testing;Integrated Security=True";
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlBulkCopy bulk = new SqlBulkCopy(con))
        {
        bulk.DestinationTableName = "MyTable";
        DataTable dt = new DataTable();
        dt.Columns.Add("MyColumnName");
        dt.Rows.Add(FirstValue);
        dt.Rows.Add(SecondValue);
        dt.Rows.Add(ThirdValue);
        bulk.WriteToServer(dt);
        }
    }
You get security and simplicity.
 
Share this answer
 
C#
string str = string.Empty;
ListItemCollection lst = lstuser1.Items;
foreach (ListItem assigned in lst)
{
    str = assigned.Text +" ,";
    string query = "insert into UserInformation(new_name) values('" + str + "')";
    SqlCommand cmd = new SqlCommand(query,c);
    c.Open();
    cmd.ExecuteNonQuery();
    c.Close();
}


-KR
 
Share this answer
 
if possible check out the topic called openxml in sqlserver.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900