Solved

multiple inserts?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Saving 5 56
which object should i use (efficiency) 5 45
Combine two datatables into one based on a value from second datatable 4 32
Syntax error 9 45
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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

17 Experts available now in Live!

Get 1:1 Help Now