Solved

multiple inserts?

Posted on 2006-06-15
5
284 Views
Last Modified: 2010-04-16
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());
            }
0
Comment
Question by:boofulls
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
gbzhhu earned 20 total points
ID: 16911598

SqlConnection sqlConn = new SqlConnection();
            sqlConn.ConnectionString = "Data Source=PC001;Initial Catalog=mytest;Integrated Security=True;";
            sqlConn.Open();

            SqlCommand cmd;
            string insertString

            foreach (string listBoxItem in listBox1.Items)
            {
                  insertString = @"insert into websites (website) values (" + listBoxItem.Trim().ToString() + ")";
                  cmd = new SqlCommand(insertString, sqlConn);
                  cmd.ExecuteNonQuery();
            }

0
 

Author Comment

by:boofulls
ID: 16911665
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();
            }
0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 16911735

Correct syntax is

            string deleteString = @"DELETE websites";
0
 

Author Comment

by:boofulls
ID: 16911776
Great thanks - and is it ok to delete the table and then do all the inserts again?

Cheers
0
 
LVL 12

Expert Comment

by:gbzhhu
ID: 16912173
Yes that is fine
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question