Solved

multiple inserts?

Posted on 2006-06-15
5
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

726 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