导航
  • 主页
  • 基础类
  • 应用实例
  • 新技术前沿

谁帮我看看这条命令到底哪里错了啊,是关于INSERT的

代码小虾米 2012-06-13 09:08:24
string sql1 = "INSERT INTO PLAYER_SOLUTION(PLAYER_SOL_ID,PLAYER_SOL,PLAYER_DATE,PLAYER_TIMEUSE(s),PLAYER_EMAIL,SUDOKU_ID) VALUES ( '" + player_sol_ID + "','" + player_Sol + "','" + date + "','" + timeUse + "','" + email + "','" + sudoku_ID + "')";

SqlCommand cmd1 = new SqlCommand(sql1, conn);
cmd1.ExecuteNonQuery();


报错说Incorrect syntax near '('.



这里除了 date 是datetime , timeuse是 int 其他都是string


...全文
61 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
代码小虾米 2012-06-17
虽然大家的答案没有解决问题,但是谢谢大家的帮助,就平均给分了。。

PS: 通过取巧解决问题了,在数据库中把字段PLAYER_TIME(s)改成PLAYER_TIME了。。。。
回复
laizhiping_rj 2012-06-13
string sql1 = "INSERT INTO PLAYER_SOLUTION(PLAYER_SOL_ID,PLAYER_SOL,PLAYER_DATE,PLAYER_TIMEUSE(s),PLAYER_EMAIL,SUDOKU_ID) VALUES ( '" + player_sol_ID + "','" + player_Sol + "','" + date.ToString() + "','" + timeUse + "','" + email + "','" + sudoku_ID + "')";
回复
發糞塗牆 2012-06-13
上面的方法我不说了,然后你的问题和int没什么关系,主要是(或者''这里出问题,我很少用编程语言,所以具体语法我不记得了,建议你可以加上一个打印的功能,先不执行,把语句打印出来放到查询分析器或者SSMS中看看有没有语法错误,一般这种拼接都是需要''xxx''甚至'''xxx'''。你要多尝试,同时建议你编写成存储过程,这样出错机会少很多。
回复
代码小虾米 2012-06-13
private void submitButton_Click(object sender, EventArgs e)
{
string player_sol_ID;
string puzzle_Sol;
string sudoku_ID = sUDOKU_IDTextBox.Text.Trim();
string email = emailTextBox.Text.Trim();
DateTime date = DateTime.Now;
string player_Sol = cLUE_1TextBox.Text.Trim() + cLUE_2TextBox.Text.Trim() + cLUE_3TextBox.Text.Trim() + cLUE_4TextBox.Text.Trim() + cLUE_5TextBox.Text.Trim() + cLUE_6TextBox.Text.Trim() + cLUE_7TextBox.Text.Trim() + cLUE_8TextBox.Text.Trim() + cLUE_9TextBox.Text.Trim() + cLUE_10TextBox.Text.Trim() + cLUE_11TextBox.Text.Trim() + cLUE_12TextBox.Text.Trim() + cLUE_13TextBox.Text.Trim() + cLUE_14TextBox.Text.Trim() + cLUE_15TextBox.Text.Trim() + cLUE_16TextBox.Text.Trim();

if (email == "")
{
MessageBox.Show("Please enter your email", "Error");
emailTextBox.Focus();
return;
}
else if (player_Sol.Length != 16)
{
MessageBox.Show("Sorry,you have not completed the PUZZLE!", "Error");
return;
}
else
{
// Write connecting string
string connstr = @"Data Source=jinbochi-pc;Initial Catalog=JuniorSudoku;Integrated Security=True";

// Create connecting object
SqlConnection conn = new SqlConnection(connstr);

// Open connecting
conn.Open();

// Check player solution.
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from PUZZLE where SUDOKU_ID=@UN";
cmd.Parameters.AddWithValue("UN", sudoku_ID);
using (SqlDataReader reader = cmd.ExecuteReader())
{
reader.Read();
puzzle_Sol = reader.GetString(1).Trim();
}

if (player_Sol == puzzle_Sol) // The solution is right.
{
// Give a unique ID to this solution.
// Write SQL string
string sql = "SELECT COUNT(PLAYER_SOL_ID) FROM PLAYER_SOLUTION";
SqlCommand cmd2 = new SqlCommand(sql, conn);
int n = int.Parse(cmd2.ExecuteScalar().ToString());
string m = (n + 1).ToString();
player_sol_ID = "PY" + m;

// insert into PLAYER_SOLUTION table.
// Write SQL string
string sql1 = "INSERT INTO PLAYER_SOLUTION (PLAYER_SOL_ID,PLAYER_SOL,PLAYER_DATE,PLAYER_TIMEUSE(s),PLAYER_EMAIL,SUDOKU_ID) VALUES ('" + player_sol_ID + "','" + player_Sol + "','" + date + "','" + timeUse + "','" + email + "','" + sudoku_ID + "')";

SqlCommand cmd1 = new SqlCommand(sql1, conn);
cmd1.ExecuteNonQuery();
conn.Close();

//Display RankForm
Form RankingForm = new RankingForm();
RankingForm.ShowDialog();
}
else
{
MessageBox.Show("Sorry,incorrect answer,Try Again!!");
return;
}

}
}
这部分的全部代码了 报错的就红色的部分

回复
代码小虾米 2012-06-13
[Quote=引用 4 楼 的回复:]

这种提示主要是语法错误,符号的半角全角或者是少了多了,更正我上面说的大小写。
[/Quote]

重新输入过了 还是报错, 而且这条命令在另一个地方就可以运行。。。。 我在想是不是数据类型的问题
回复
FeelTouch 2012-06-13
这种提示主要是语法错误,符号的半角全角或者是少了多了,更正我上面说的大小写。
回复
FeelTouch 2012-06-13
楼主,你检查下'(',附近的标点符号的,大小写,再试试
回复
代码小虾米 2012-06-13
[Quote=引用 1 楼 的回复:]

string sql1 = "INSERT INTO PLAYER_SOLUTION(PLAYER_SOL_ID,PLAYER_SOL,PLAYER_DATE,[PLAYER_TIMEUSE(s)],PLAYER_EMAIL,SUDOKU_ID) VALUES ( '" + player_sol_ID + "','" + player_Sol + "','" + date.ToLongStrin……
[/Quote]


加完 还是报错啊 Invalid column name 'PLAYER_TIMEUSE(s)'.
回复
SQL777 2012-06-13
string sql1 = "INSERT INTO PLAYER_SOLUTION(PLAYER_SOL_ID,PLAYER_SOL,PLAYER_DATE,[PLAYER_TIMEUSE(s)],PLAYER_EMAIL,SUDOKU_ID) VALUES ( '" + player_sol_ID + "','" + player_Sol + "','" + date.ToLongString() + "','" + timeUse + "','" + email + "','" + sudoku_ID + "')";
回复
发动态
发帖子
MS-SQL Server
创建于2007-09-28

3.2w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
社区公告
暂无公告