简体   繁体   中英

Syntax error in SQL for create a table C#

Hallo ladies and gentlemen

I am strugling with what I think should be a easy solution but can't find it

Basically I am creating a table through SQL and there is a problem with the SQL when I run it. Here is my code:

OleDbCommand cmd = new OleDbCommand (@"CREATE TABLE " + ClientID + " (Transaction_ID INT PRIMARY KEY NOT NULL, Transaction CHAR(255), Date DATE, Transaction_Amount INT, Balance INT, Deposited INT, Withdrew INT, Loan INT, Applied INT, Approved INT, Payment_Monthly INT, Monthly_Income INT)", mydb);

Now I tested my SQL statement to found out which part of my SQL gives the error and I have found when I remove the fields Transaction and Date it works. If I remove only one of them it still crashes. I even tried to change data types like CHAR(255) to VARCHAR(255) also DATE to INT .

To be honest I can't seem to find the problem. Please help

Thank you for your time.

PS: ClientID is a combination of three numbers and three letters with a hashtag for example. 123#ABC

Those two column names are reserved words in SQL. Enclosing them in square brackets will solve your problem:

[Transaction] CHAR(255), 
[Date] DATE,

Keep in mind that any time you access these fields in dynamic SQL or stored procedures, you will have to use the square brackets as well, ie

SELECT * FROM ClientTable WHERE [Transaction] = 'A'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM