Reputation: 1
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.
Upvotes: 0
Views: 183
Reputation: 9460
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.
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.
Upvotes: 1
Reputation: 446
string query = "INSERT INTO [Table] VALUES(3, 'Sachin', '120000', 'Nagaur')";
Upvotes: 0
Reputation: 13484
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')";
Upvotes: 0
Reputation: 5258
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.
Upvotes: 0
Reputation: 6590
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')";
Upvotes: 0
Reputation: 5947
Table is a default SQL keyword. Give specific name to your table.
INSERT INTO Customer VALUES(3, 'Sachin', '120000', 'Nagaur')
Upvotes: 0
Reputation: 28771
Table
is keyword , can't be used as tablename.
Change query to
string query = "INSERT INTO yourTableName VALUES(3, 'Sachin', '120000', 'Nagaur')";
Upvotes: 4