关于此SQL语句有什么问题的任何建议? [英] Any suggestions on what is wrong with this SQL statement?

查看:76
本文介绍了关于此SQL语句有什么问题的任何建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了这小段代码,似乎遇到了种种麻烦!我知道这只是简单的事情,但是我得到的错误消息几乎没有用,因为我认为我的SQL语句是正确的.如果有人可以找我,将不胜感激.

Hi, I have written this small piece of code and seem to be having all kinds of trouble with it! I know this is something simple I am just looking over but the error message I am getting is pretty much useless as I think my SQL statement is correct. If someone could have a look for me it would be appreciated.

private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            SqlConnection conn = new SqlConnection(Calorie_Counter_UK.Properties.Settings.Default.Calorie_CounterConnectionString);
            conn.Open();
            try
            {
                using (SqlCommand command = new SqlCommand("INSERT INTO User (Name,Age,Height,Weight,Weight_Target) VALUES (@Name,@Age,@Height,@Weight,@Target)", conn)) ;
                {
                    command.Parameters.Add(new SqlParameter("Name", txtName.Text));
                    command.Parameters.Add(new SqlParameter("Age", txtName.Text));
                    command.Parameters.Add(new SqlParameter("Height", txtName.Text));
                    command.Parameters.Add(new SqlParameter("Weight", txtName.Text));
                    command.Parameters.Add(new SqlParameter("target", txtName.Text));
                    Command.ExecuteNonQuery();
                }
            }
            catch
            {
                MessageBox.Show("Could not Insert");
            }
        }

>

在命令上.ExecuteNonQuery();我收到错误-关键字用户"附近的语法不正确".现在,我已经看过了,但是对我来说,insert语句的语法是正确的.如果有人可以建议的话,将不胜感激!

感谢

>

On command.ExecuteNonQuery(); I get the error - "Incorrect syntax near the keyword ''User''". Now I have looked and looked but to me the syntax for insert statement is correct. If anyone could advise it would be appreciated!

Thanks

推荐答案

其中某些表或列名称可能是数据库引擎中的保留字. (我会怀疑用户名和名称.)您必须在查询中转义这些名称,例如,对于mySQL来说是"name",对于SQL Server来说是"[name]"(如果我记得正确的话).
Some of those table or column names are probably reserved words in your database engine. (User and Name I would be suspicious of.) You have to escape those in the query, either like `name` for mySQL or [name] for SQL server (if I''m remembering that right).


尝试通过以下方式找出

try to find out in the following way

INSERT INTO [User] (Name,Age,Height,Weight,Weight_Target) VALUES (@Name,@Age,@Height,@Weight,@Target)


这篇关于关于此SQL语句有什么问题的任何建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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