它给我语法错误请帮助我,我不知道问题 [英] Its giving me syntax error please help me I dont know the problem

查看:76
本文介绍了它给我语法错误请帮助我,我不知道问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它给我语法错误请帮帮我,我不知道问题

its giving me syntax error please help me i dont know the problem

con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update product set LastName=@LastName,FirstName=@FirstName,MiddleName=@MiddleName,Section=@Section where StudentID=@StudentID";
            cmd.Parameters.Clear();
            cmd.Parameters.AddWithValue("@LastName", TextBox1.Text);
            cmd.Parameters.AddWithValue("@FirstName", TextBox2.Text);
            cmd.Parameters.AddWithValue("@MiddleName", TextBox3.Text);
            cmd.Parameters.AddWithValue("@Section", TextBox4.Text);
          //  cmd.Parameters.AddWithValue("@StudentID", TextBox5.Text);
            cmd.ExecuteNonQuery();





我尝试过:



i不知道问题





What I have tried:

i dont know the problem

con.Open();
            OleDbCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "UPDATE product SET LastName='" + TextBox1.Text+ "',FirstName='" + TextBox2.Text + "',MiddleName='" + TextBox3.Text + "',Section='"+TextBox4.Text+"' where StudentID=" + TextBox5.Text + "";
            cmd.ExecuteNonQuery();
            this.Refresh();

            con.Close();

这是我的第一个代码

推荐答案

好事是你有摆脱了连接文本版本 - 这是非常危险的!



接下来要做的就是找出语法错误 - 我就是猜测它是在运行时而不是编译时间,这意味着它是SQL命令的问题。

这意味着它可能是命名参数,OleDbConnection不支持这样的参数。 SqlConnection。

因为您在查询中命名了一个参数,但在 cmd.Parameters.AddwithValue 列表中将其注释掉,tejh命令处理器可能是抱怨< pre>其中StudentID = @ StudentID< / pre> 因为 @StudentID 将被替换有一个NULL,这可能是问题



取消注释该行,它应该工作:

The good thing is that you have got rid of the concatenated text version - that is very dangerous!

The next thing to do is find out when you get "a syntax error" - and I'm guessing it's at runtime not compile time, which means it's a problem with the SQL command.
And that means it's likely to be the named parameters, which an OleDbConnection doesn't support in the same way as an SqlConnection.
Because you name a parameter in your query, but comment it out in your cmd.Parameters.AddwithValue list, tejh command processor is probably complainign about the "<pre>where StudentID=@StudentID</pre>" becuase @StudentID will be replaced with a NULL and that's probably the problem

Uncomment the line, and it should work:
cmd.CommandText = "update product set LastName=@LastName,FirstName=@FirstName,MiddleName=@MiddleName,Section=@Section where StudentID=@StudentID";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@LastName", TextBox1.Text);
cmd.Parameters.AddWithValue("@FirstName", TextBox2.Text);
cmd.Parameters.AddWithValue("@MiddleName", TextBox3.Text);
cmd.Parameters.AddWithValue("@Section", TextBox4.Text);
cmd.Parameters.AddWithValue("@StudentID", TextBox5.Text);
cmd.ExecuteNonQuery();

但请记住,这些名称在OldDb中无关紧要,可以用占位符替换 - 添加它们的顺序控制它们的去向!

But do remember that the names are irrelevant in OldDb, and can be replaced with placeholders - the order in which they are added controls where they go!

cmd.CommandText = "UPDATE product SET LastName=@LastName, FirstName=?, MiddleName=?, Section=? WHEREStudentID=?";
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@LastName", TextBox1.Text);
cmd.Parameters.AddWithValue("@FirstName", TextBox2.Text);
cmd.Parameters.AddWithValue("@MiddleName", TextBox3.Text);
cmd.Parameters.AddWithValue("@Section", TextBox4.Text);
cmd.Parameters.AddWithValue("@StudentID", TextBox5.Text);
cmd.ExecuteNonQuery();


这篇关于它给我语法错误请帮助我,我不知道问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆