2013-04-11 52 views
-1

我想從用戶的數據表插入數據。我得到這個異常:「異常:在事務附近的語法不正確」。我試圖找到語法錯​​誤,但我無法解決它。如何解決「交易」附近的錯誤語法?

public void Add_to_Transaction(SqlConnection conn , int serial_number , DataTable dt) 
{ 
    try 
    { 
     //DataTable dt = new DataTable(); 
     SqlCommand cmd1 = new SqlCommand(); 
     cmd1.Connection = conn; 
     cmd1.CommandText = "SELECT * FROM Transaction"; 
     SqlDataAdapter adapter = new SqlDataAdapter(); 
     adapter.SelectCommand = cmd1; 
     adapter.Fill(dt); 

     SqlParameter serial_number1 = new SqlParameter("product_id",serial_number); 
     SqlParameter quantity = new SqlParameter("quantity", 0); 
     SqlParameter date = new SqlParameter("date", DateTime.Today); 
     cmd1.Parameters.Add(serial_number1); 
     cmd1.Parameters.Add(quantity); 
     cmd1.Parameters.Add(date); 
     Console.WriteLine("111111111111111111"); 

     cmd1.CommandText = "INSERT INTO Transaction (quantity,date,product_id) VALUES (@quantity,@date,@product_id)"; 
     cmd1.ExecuteNonQuery(); 

     Console.WriteLine("222222222222222222"); 

     cmd1.Parameters.Clear(); 
    } 

    catch (Exception e) 
    { 
     Console.WriteLine(e.Message); 
    } 
} 
+0

「Tatabransaction」?你在這裏或在你的程序中輸入錯了嗎? – tnw 2013-04-11 20:07:23

+1

大海撈針出現在腦海中......什麼原因導致了錯誤? – Sayse 2013-04-11 20:07:53

+3

你有一個流浪'之前@product_id – 2013-04-11 20:08:36

回答

0
SELECT * FROM [Transaction] 

[ ]把詞彙,SQL知道這是不是一個關鍵字。

3

T-SQL中有保留關鍵字的詞。你不能直接在sql中使用它們,不要在它們周圍使用括號。所以,要麼選擇這些列或表名或使用括號[forbiddenword]他們周圍不同的名字:

INSERT INTO [Transaction] (quantity,date,product_id) VALUES (@quantity,@date,@product_id); 
SELECT * FROM [Transaction]; 

Reserved Keywords (Transact-SQL)

+0

謝謝蒂姆!!!!!!! – SmadjaM 2013-04-11 21:28:03

+0

謝謝大家! – SmadjaM 2013-04-11 23:58:18

0

Transaction是一個關鍵的詞。 這是你的意思嗎?