拒绝制作ExecuteNonQuery:'('附近的语法不正确

时间:2012-01-03 16:56:46

标签: asp.net sql-server database

我试图将数据插入我的数据库,它给了我一个错误:

  

'('。

附近的语法不正确

这是我的代码:

string username = Session["Session"].ToString();

            con = new SqlConnection("Data Source=MICROSOF-58B8A5\\SQL_SERVER_R2;Initial Catalog=Daniel;Integrated Security=True");
            con.Open();

            string knowWhichOne = "SELECT ID FROM Users WHERE Username='" + UserOrGuest.Text + "'";

            SqlCommand comm = new SqlCommand(knowWhichOne, con);
            int userID = (Int32)comm.ExecuteScalar();

            knowWhichOne = "SELECT ClassID FROM Users WHERE Username='" + UserOrGuest.Text + "'";

            comm = new SqlCommand(knowWhichOne, con);
            int classID = (Int32)comm.ExecuteScalar();

            knowWhichOne = "SELECT SchoolID FROM Users WHERE Username='"+UserOrGuest.Text + "'";

            comm = new SqlCommand(knowWhichOne, con);
            int schoolID = (Int32)comm.ExecuteScalar();

            if (RadioWords.Checked == true)
            {
                 game = 1;
            }
            else
            {
                 game = 2;
            }


            string sqlqueryString = "INSERT INTO (GameID, UserID, LengthOfArray, NumberOfErrors, ClassID, SchoolID) VALUES (@GameID, @UserID, @LengthOfArray, @NumberOfErrors, @ClassID, @SchoolID)";

            SqlCommand commandquery = new SqlCommand(sqlqueryString, con);

            commandquery.Parameters.AddWithValue("GameID", game);
            commandquery.Parameters.AddWithValue("UserID", userID);
            commandquery.Parameters.AddWithValue("LengthOfArray", HowMany.Text);
            commandquery.Parameters.AddWithValue("NumberOfErrors", 0);
            commandquery.Parameters.AddWithValue("ClassID", classID);
            commandquery.Parameters.AddWithValue("SchoolID", schoolID);

            commandquery.ExecuteNonQuery();
            con.Close();

我在调试模式下运行它,它接受所有内容,直到“ExecuteNonQuery();”线。

任何人都知道我做错了什么?

谢谢!

3 个答案:

答案 0 :(得分:8)

你这样做了:

INSERT INTO (GameID....

但应该这样做:

INSERT INTO tablename (GameID....

答案 1 :(得分:3)

您的INSERT INTO语句缺少它应插入的表的名称。

答案 2 :(得分:2)

insert into语句的语法不正确,因为您没有指定要插入的表。

正确的语法是

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

有关详细信息,请参阅:http://www.w3schools.com/sql/sql_insert.asp