Solved

multiple inserts?

Posted on 2006-06-15
5
260 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now