0

I am learning C# and I am trying to connect my program to a SQL Server database. For the learning purpose I followed the following video :

http://www.youtube.com/watch?v=Rwdedptaou0

This is my code:

using System.Data.SqlClient;

namespace TestDBMS
{
    public partial class MainWindow : Window
    {
        SqlConnection conn;
        SqlCommand cmd;

        public MainWindow()
        {
            InitializeComponent();
            conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Reventon\Documents\EmployeeDB.mdf;Integrated Security=True;Connect Timeout=30");
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string query = "INSERT INTO Table VALUES(3, 'Sachin', '120000', 'Nagaur')";
            cmd = new SqlCommand();
            cmd.CommandText = query;
            cmd.Connection = conn;
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            MessageBox.Show("Record Saved");
        }
    }
}

After compiling the program I am getting an error

Incorrect Syntax Near the 'Table'

(for better representation i have captured the screenshot and uploaded on my Skydrive account). http://sdrv.ms/1bUDe3l

Please help me out.

4
  • I tried changing Table name but everything i am changing the name and pressing the save button the Save Dialogue Box is keep appearing to enter name again and again. Please checkout the ScreenShot : sdrv.ms/LTvowG Commented Feb 12, 2014 at 5:23
  • @press update dont press save..!! :) it works..:) Commented Feb 12, 2014 at 6:26
  • run the query which i gave you.. it works Commented Feb 12, 2014 at 6:27
  • i have updated my answer, check for it..! Commented Feb 12, 2014 at 6:29

7 Answers 7

4

Table is keyword , can't be used as tablename. Change query to

 string query = "INSERT INTO yourTableName VALUES(3, 'Sachin', '120000', 'Nagaur')";
Sign up to request clarification or add additional context in comments.

3 Comments

But sir, it was the default name of the table, Should i change it to any anonymous name?
Yes sir, change table name in database and then in code, the word Table is a SQL keyword .
Unable to Change Table name : I tried changing Table name but everything i am changing the name and pressing the save button the Save Dialogue Box is keep appearing to enter name again and again. Please checkout the ScreenShot : sdrv.ms/LTvowG
1

Insert Query Syntax:

INSERT INTO yourtablename
(column1, column2, ... )
VALUES
(expression1, expression2, ... );

The error in your code is the table rename it to the appropriate table name like Customertable

INSERT INTO Customertable VALUES(3, 'Sachin', '120000', 'Nagaur')

Alter table name:

Use sp_rename to rename your table name:

    EXEC sp_rename 'table', 'Customertable'

    sp_rename old_table_name , new_table_name

ALTER TABLE TABLE_NAME RENAME TO NEW_TABLE_NAME Works only in Oracle DB.

Tutorial

You can find documentation on this procedure on MSDN.

In Server Explorer right click on Views and click New Query.
use this code to rename table:

EXEC sp_rename 'Table', 'NewName'  

then click on Execute button.
after 5-30 seconds in server explorer click on refresh button.

enter image description here

5 Comments

Do i need to use SQL Server, Isn't it possible to change it via Visual Studio only?
no to change the table name you should be using the database engine..!! i.e sql server
Yesssssssssssssssssssssssssssssssssssssssssssssssss. Its Done :) I am happy
Thank you so much. Sir can you guide me that which book i should learn to know this connectivity aspects?
Glat that it helped..! :) Happy coding..! :)
0

Table is a default SQL keyword. Give specific name to your table.

INSERT INTO Customer VALUES(3, 'Sachin', '120000', 'Nagaur')

3 Comments

How can i change the table name?
Here You can see how to change the table name!
ALTER TABLE table_name RENAME TO new_table_name;
0

You can not use Table keyword as tablename. You should give some other name. Here is list of keywords in SQL Server: http://technet.microsoft.com/en-us/library/aa238507%28v=sql.80%29.aspx

Try this query :

string query = "INSERT INTO yourTableName VALUES (3, 'Sachin', '120000', 'Nagaur')";

OR

string query = "INSERT INTO yourTableName(Column1, Column2, Column3, Column4) VALUES (3, 'Sachin', '120000', 'Nagaur')";

3 Comments

Unable to Change Table name : I tried changing Table name but everything i am changing the name and pressing the save button the Save Dialogue Box is keep appearing to enter name again and again. Please checkout the ScreenShot : sdrv.ms/LTvowG
Change your table name to DemoTable and in Insert query also
But I see it in you database. change it in database also
0

Actually while all but one of the answers are correct, you CAN actually use the word Table as a table name, if you use square brackets [ ]

Create Table Table ( id int ) -- doesn't work!

However,

Create Table [Table] ( id int ) -- works!

I don't recommend this practice and agree it's best to use a name other than "Table" even with square brackets for your database table!

Just wanted to point out that technically speaking, there is actually a way to use the SQL keyword 'table' as a table name in SQL Server.

Comments

0

Can you change your tablename?? and try again.Because table is a default keyword

INSERT INTO Tablename (col1,col2,col3,col4) VALUES(3, 'Sachin', '120000', 'Nagaur')";

Comments

0
string query = "INSERT INTO [Table] VALUES(3, 'Sachin', '120000', 'Nagaur')";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.