2015-04-19 116 views
2

我在將同一行中的整數值[問題類型]和字符串[問題空間]同時插入到我的數據庫時遇到問題。每當我按一下按鈕,並嘗試執行一個錯誤出現說:將整數和字符串插入到SQL Server數據庫c中的問題#

類型「System.Data.SqlClient.SqlException」未處理的異常出現在system.data.dll

其他信息:不正確語法附近 '('

代碼:。

SqlCommand command5 = new SqlCommand("INSERT INTO ([Question Space], [Question Type]) Questions VALUES ('@QuestionText', '@checkedradiobutton')", connect); 
command5.Parameters.AddWithValue("@QuestionText", QuestionText); 
command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton); 

command5.ExecuteNonQuery(); 

我希望得到任何幫助,任何人都可以給我

這裏的按鈕的整個代碼,如果你想看到:

private void button1_Click(object sender, EventArgs e) 
{ 
    string connectionString = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString; 

    SqlConnection connect = new SqlConnection(connectionString); 
    connect.Open(); 

    int checkedradiobutton = 0; 

    if(radioButton1.Checked) 
    { 
     checkedradiobutton = 1; 
    } 
    else if(radioButton2.Checked) 
    { 
     checkedradiobutton = 2; 
    } 
    else if(radioButton3.Checked) 
    { 
     checkedradiobutton = 3; 
    } 

    string QuestionText = QuestionBox.Text; 

    SqlCommand command5 = new SqlCommand("INSERT INTO ([Question Space], [Question Type]) Questions VALUES ('@QuestionText', '@checkedradiobutton')", connect); 
    command5.Parameters.AddWithValue("@QuestionText", QuestionText); 
    command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton); 

    command5.ExecuteNonQuery(); 
} 

而且我的數據庫表:

CREATE TABLE [dbo].[Questions] 
(
    [QuestionID]  INT   IDENTITY (1, 1) NOT NULL, 
    [Actual answer] NVARCHAR (50) NULL, 
    [Question Space] NVARCHAR (50) NULL, 
    [Question Type] INT   NULL, 

    PRIMARY KEY CLUSTERED ([QuestionID] ASC) 
); 

回答

6

用於插入正確的語法是

INSERT INTO <table> (field1, field2, fieldN) VALUES (value1, value2, valueN) 

所以你的命令應該寫成

SqlCommand command5 = new SqlCommand(@"INSERT INTO Questions 
         ([Question Space], [Question Type]) VALUES 
         (@QuestionText, @checkedradiobutton)", connect); 

注意的參數佔位符怎麼不應該用單引號括起來,因爲這樣做可以用文字轉換它們。 (插入的"@QuestionText"串在你[Question Space]場)

最後,儘量避免AddWithValue,它與很多缺點的快捷方式,你可以在Can we stop using AddWithValue already讀取和How Data Access Code Affects Database Performance

這仍然是代碼

一行
command5.Parameters.Add("@QuestionText", SqlDbType.NVarChar).Value = QuestionText; 
+0

非常感謝!像一種享受! – mot375

+1

@ mot375:如果您覺得此答案可幫助您解決問題,請[**接受此答案**](http://meta.stackoverflow.com/q/5234/153998)。這將表明你對那些花時間幫助你的人表示感謝。 –

2

你不必用'包裹在查詢的參數。開始清理它,看看它是否工作

SqlCommand command5 = new SqlCommand("INSERT INTO [dbo].[Questions] ([Question Space], [Question Type]) Questions VALUES (@QuestionText, @checkedradiobutton)", connect); 
    command5.Parameters.AddWithValue("@QuestionText", QuestionText); 
    command5.Parameters.AddWithValue("@checkedradiobutton", checkedradiobutton); 


    command5.ExecuteNonQuery(); 
1

的INSERT語法是錯誤的,該表的名稱應 「INSERT INTO」 後,例如去:

INSERT INTO table_name (column1,column2,column3,...) 
VALUES (value1,value2,value3,...); 

乾杯。

相關問題