Solved

updating SQL server through C# vs2010

Posted on 2012-03-27
8
414 Views
Last Modified: 2012-06-27
Hi

I have two identical tables.
I use one as a temporary table for importing data to, so it can be manipulated before updating the real table.

I've tried updaing the real table with:

 string SQL = "INSERT INTO ActualReadings " +
                                " (Date,Time,Thing1, thing2, thing3)" +
                        "SELECT  "  +
                        "(Date,Time,Thing1, thing2, thing3)" +
                            " FROM TempReadings";

                //Declare the Command
                SqlCommand cmd = new SqlCommand(SQL, conn);

                //Make the connection
                conn.Open();
               
                //Execute query
                int NumRows = 0;
                NumRows = cmd.ExecuteNonQuery();
               
but get an error, that I believe is related to the fact both tables have an identity column (ID).

What's the best way around this?

Many thanks.
0
Comment
Question by:Spike_66
  • 5
  • 3
8 Comments
 
LVL 10

Expert Comment

by:plummet
ID: 37772635
Hi

Your SQL looks OK to me, you're not trying to insert an identity value unless one of your fields Date, Time, Thing1 etc are an identity field.

What is the error you're getting?
0
 

Author Comment

by:Spike_66
ID: 37776413
Incorrect syntax near ','.
 which is refering to the first "," after the word select.

i'm sure it's something to do with both tables having identity columns (ID) which I'm not inserting but obviuosly want to generate.
The column is set up correctly, but just doesn't want to work on a bulk inport.
0
 
LVL 10

Accepted Solution

by:
plummet earned 500 total points
ID: 37776429
Try sticking [ and ] around Date and Time, as they are reserved words, and remove those brackets:

string SQL = "INSERT INTO ActualReadings " +
                                " ([Date],[Time],Thing1, thing2, thing3)" +
                        "SELECT  "  +
                        "[Date],[Time],Thing1, thing2, thing3 " +
                            " FROM TempReadings";

Open in new window


Hopefully that will fix it
0
 

Author Comment

by:Spike_66
ID: 37776587
Many thanks- I noticed the brackets just  before you replied and removing them fixed it.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 10

Expert Comment

by:plummet
ID: 37776605
Yep, simple as that, not an issue with identity. I'm glad to have helped

Cheers
John
0
 
LVL 10

Expert Comment

by:plummet
ID: 37779285
Hi Spike

Are you going to allocate points for my answer?

Many thanks
John
0
 

Author Comment

by:Spike_66
ID: 37781149
Apologies- I thought I had done it (It must be old age!!)
0
 
LVL 10

Expert Comment

by:plummet
ID: 37781255
I quite understand, judging by your name on here I am 3 years older than you. Now where did I put my glasses...
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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

708 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

19 Experts available now in Live!

Get 1:1 Help Now