简体   繁体   English

SqlException:'1'附近的语法不正确

[英]SqlException : Incorrect syntax near '1'

I am currently trying to implement SQL into a project with Unity3D. 我目前正在尝试使用Unity3D将SQL实施到项目中。 So far, I was able to do "normal" UPDATE, ADD, DELETE, DROP, ALTER, INSERT". 到目前为止,我已经能够执行“正常”的UPDATE,ADD,DELETE,DROP,ALTER,INSERT”。

Trying to go a step further, I am trying to insert prepared statements, using this link as a guide 尝试进一步,我尝试使用此链接作为指南插入准备好的语句

Here is my code : 这是我的代码:

SqlConnection sqlConnection = new SqlConnection(Connection.connectionString)
sqlConnection.Open();

SqlCommand cmd = new SqlCommand(null, sqlConnection);
cmd.CommandText = "INSERT INTO IngredientTypes (Name) VALUES (@name)";

SqlParameter nameParam = new SqlParameter("@name", SqlDbType.Text, 155);
nameParam.Value = Name;
cmd.Parameters.Add(nameParam);

cmd.Prepare();
cmd.ExecuteNonQuery();

My table looks like so : 我的桌子看起来像这样:

CREATE TABLE IngredientTypes
(
    IngredientTypeID INT IDENTITY(1,1) PRIMARY KEY,
    Name VARCHAR(155)
);

I get this error : 我收到此错误:

SQLException : Incorrect systax near '1'. SQLException:'1'附近的系统税不正确。

System.Data.SqlClient.SqlConnection.ErrorHandler (System.Object sender, Mono.Data.Tds. Protocol.TdsInternalErrorMessageEventArgs e) System.Data.SqlClient.SqlConnection.ErrorHandler(System.Object发件人,Mono.Data.Tds.Protocol.TdsInternalErrorMessageEventArgs e)

Help please? 请帮助? Thank you in advance.. I can't find where I did wrong. 在此先感谢您..我找不到我做错了什么。

You can reduce that code quite a bit with no loss of function, and even some important improvements (for example, this will close the connection even if an exception is thrown): 您可以在不损失功能的情况下进行大量减少,甚至可以进行一些重要的改进(例如,即使抛出异常,这也会关闭连接):

using (var sqlConnection = new SqlConnection(Connection.connectionString))
using (var cmd = new SqlCommand("INSERT INTO IngredientTypes (Name) VALUES (@name)",  sqlConnection))
{
    cmd.Parameters.Add("@name", SqlDbType.VarChar, 155).Value = Name;
    sqlConnection.Open();
    cmd.ExecuteNonQuery();
}

I'm not sure what's causing that exception in your existing code, though, because 1 is not used anywhere in that query. 不过,我不确定是什么导致了您现有代码中的异常,因为该查询中的任何地方都没有使用1 I suspect the problem has something to do with SqlDbType.Text , since that is not the correct type to use with a VarChar column, but it seems just as likely there's code somewhere we haven't seen yet that's changing your SQL command text. 怀疑问题与SqlDbType.Text ,因为这不是VarChar列一起使用的正确类型,但是似乎还有一些代码尚未出现,这正在改变您的SQL命令文本。

Definitely the Prepare() method in your link is not needed for Sql Server. 无疑,SQL Server不需要链接中的Prepare()方法 It's inherited here from DbCommand , where it's included because it's an important part of the API for some other databases, but Sql Server has handled this automatically for more than 10 years now. 它是从DbCommand继承而来的,因为它是其他一些数据库的API的重要组成部分,所以它被包含在DbCommand ,但是Sql Server已经自动处理了10多年了。

SqlDbType.Text Is not the same as varchar. SqlDbType.Text与varchar不同。 I don't believe Text types have a length you specify. 我不相信您指定的文本类型的长度。

Could you try below? 您可以在下面尝试吗? Using the "using" structure is safer for sql connections by the way, the connection automatically closes when your process is done. 顺便说一下,对于SQL连接,使用“使用”结构更安全,当过程完成时,连接会自动关闭。

using (SqlConnection sqlConnection = new SqlConnection(Connection.connectionString))
    {
        SqlCommand command = new SqlCommand("INSERT INTO IngredientTypes (Name) VALUES (@name)", connection);
        command.Parameters.Add("@name", SqlDbType.Varchar, 155);
        command.Parameters["@name"].Value = Name; //make sure Name is string.

        try
        {
            sqlConnection.Open();
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

I tried your code exactly as it is and found no issue. 我完全按原样尝试了您的代码,但没有发现问题。 Though there are few compilation errors (missing ; in line 1 and Name variable should be coming as parameter) but I am sure you know that. 尽管几乎没有编译错误(缺少;在第1行中,名称变量应该作为参数出现),但是我相信您知道这一点。 If you have posted your table structure and code exactly the same as you have in your project, then there is no problem in this code. 如果您发布的表结构和代码与项目中的代码完全相同,则此代码没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM