Link to home
Start Free TrialLog in
Avatar of boofulls
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().ToString());
            }
ASKER CERTIFIED SOLUTION
Avatar of gbzhhu
gbzhhu
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of boofulls
boofulls

ASKER

Would it be wise to do this DELETE part too?

Is it the correct syntax?

I got an error Incorrect syntax near *



SqlConnection sqlConn = new SqlConnection();
            sqlConn.ConnectionString = "Data Source=PC024;Initial Catalog=paulstest;Integrated Security=True;";
            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().ToString() + ")";
                cmd = new SqlCommand(insertString, sqlConn);
                cmd.ExecuteNonQuery();
            }

Correct syntax is

            string deleteString = @"DELETE websites";
Great thanks - and is it ok to delete the table and then do all the inserts again?

Cheers
Yes that is fine