boofulls
asked on
multiple inserts?
Hi
I can get this to insert 'this is a test' and that works fine.
But how should I get it to insert the data that Im showing in the MessageBox in the foreach loop?
Thanks
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = "Data Source=PC001;Initial Catalog=mytest;Integrated Security=True;";
sqlConn.Open();
string insertString = @"insert into websites (website) values ('This is a test')";
SqlCommand cmd = new SqlCommand(insertString, sqlConn);
cmd.ExecuteNonQuery();
foreach (string listBoxItem in listBox1.Items)
{
MessageBox.Show("saving " + listBoxItem.Trim().ToStrin g());
}
I can get this to insert 'this is a test' and that works fine.
But how should I get it to insert the data that Im showing in the MessageBox in the foreach loop?
Thanks
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = "Data Source=PC001;Initial Catalog=mytest;Integrated Security=True;";
sqlConn.Open();
string insertString = @"insert into websites (website) values ('This is a test')";
SqlCommand cmd = new SqlCommand(insertString, sqlConn);
cmd.ExecuteNonQuery();
foreach (string listBoxItem in listBox1.Items)
{
MessageBox.Show("saving " + listBoxItem.Trim().ToStrin
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Correct syntax is
string deleteString = @"DELETE websites";
ASKER
Great thanks - and is it ok to delete the table and then do all the inserts again?
Cheers
Cheers
Yes that is fine
ASKER
Is it the correct syntax?
I got an error Incorrect syntax near *
SqlConnection sqlConn = new SqlConnection();
sqlConn.ConnectionString = "Data Source=PC024;Initial Catalog=paulstest;Integrat
sqlConn.Open();
string deleteString = @"DELETE * FROM websites";
SqlCommand cmd = new SqlCommand(deleteString, sqlConn);
cmd.ExecuteNonQuery();
string insertString;
foreach (string listBoxItem in listBox1.Items)
{
insertString = @"insert into websites (website) values (" + listBoxItem.Trim().ToStrin
cmd = new SqlCommand(insertString, sqlConn);
cmd.ExecuteNonQuery();
}