Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Each time when I press my button it says

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Incorrect syntax near the keyword 'DataBase'.

Incorrect syntax near the keyword 'SET'.

Could you help me?

What I have tried:

private void button1_Click(object sender, EventArgs e)
{
  con = new SqlConnection(@"Data Source=        (LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\djino_5yu1pcr\Documents\DataSet.mdf;I  ntegrated Security=True;Connect Timeout=30");
  con.Open();
  cmd = new SqlCommand("Update DataBase Set (Serial,Name) VALUES (@Serial,@Name)", con);
  cmd.Parameters.Add("@Serial", txtSerial.Text);
  cmd.Parameters.Add("@Name", txtName.Text);
  cmd.ExecuteNonQuery();
}
Posted
Updated 3-Jan-17 4:41am
v2
Comments
[no name] 3-Jan-17 10:24am    
http://www.w3schools.com/sql/sql_update.asp
Member 12932743 3-Jan-17 10:27am    
Can you use Insert and into?
[no name] 3-Jan-17 14:37pm    
Did you try?

You've made an UPSERT statement (which isn't a thing that exists). Your UPDATE statement is wrong...it looks like you mixed the formatting of INSERT and UPDATE. It should be formatted as follows

SQL
UPDATE Database SET Serial = @Serial, Name = @Name;


So that would be
UPDATE <TableName> SET <ColumnName> = <ColumnValue>,<ColumnName> = <ColumnValue>


or for single column updates

UPDATE <TableName> SET <ColumnName> = <ColumnValue>
 
Share this answer
 
v2
Comments
Member 12932743 3-Jan-17 10:36am    
I tried the insert method but it said Additional information: Incorrect syntax near the keyword 'DataBase'.
David_Wimbley 3-Jan-17 10:40am    
That is fine, but based on the code sample you provided in your question the way you've formatted that UPDATE is not correct syntax. My answer provides you correct syntax.

Also to be clear, you can't update a database, you have to specify the table within the database to update.

So assuming you are using SQL Server if you have a database called Company and a table called Employees...if you are trying to update the Employees table you can't do
UPDATE Company SET Name = 'David' WHERE Id = 1
you have specify the table such as
UPDATE Employees SET Name = 'David' WHERE id = 1
. You can specify the database if you wish but that should be specified in your connection string.
Assuming that you are trying to update a table called DataBase, there are two problems:

1) Your UPDATE command syntax is wrong (See the link provided by NPC in the comments).

2) Database is a reserved word in SQL. It's okay to use reserved words as object names but you'll need to escape them with square brackets when you refer to them.

As such, your SQL should look like this:

SQL
UPDATE [DataBase] SET Serial = @Serial, Name = @Name


Note that that will update every row in the table as there is no WHERE clause.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900