我在將同一行中的整數值[問題類型]和字符串[問題空間]同時插入到我的數據庫時遇到問題。每當我按一下按鈕,並嘗試執行一個錯誤出現說:將整數和字符串插入到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)
);
非常感謝!像一種享受! – mot375
@ mot375:如果您覺得此答案可幫助您解決問題,請[**接受此答案**](http://meta.stackoverflow.com/q/5234/153998)。這將表明你對那些花時間幫助你的人表示感謝。 –