错误“'nvarchar'附近的语法不正确。”尝试在存储过程的表中插入行int

时间:2016-08-24 14:30:54

标签: c# sql-server stored-procedures

我一直坚持这个错误一小时,现在我迫切需要别人的帮助。我不知道如何找到错误,所以我只会在这里展示我编码的内容。

存储过程:

CREATE PROCEDURE AddProduct @id nvarchar(100), @name nvarchar(100), @price money, 
                         @type int
    AS
    BEGIN
    SET NOCOUNT ON;
        BEGIN TRY
        BEGIN TRANSACTION;
                INSERT INTO Product (product_id, product_name, product_price, [type_id])
                VALUES(@id, @name, @price, @type);
        COMMIT;
        END TRY
        BEGIN CATCH
            ROLLBACK;
        END CATCH
    END;
GO

这是我的C#功能

public void Add()
    {
        string command = "EXEC AddProduct @id nvarchar(100), @name nvarchar(100), @price money, @type int";
        var query = Connection.InitSqlCommand(command);
        query.Parameters.AddWithValue("@id", Id);
        query.Parameters.AddWithValue("@name", Name);
        query.Parameters.AddWithValue("@price", Price);
        query.Parameters.AddWithValue("@type", Type);
        Connection.ExecuteQuery(query);    
    }

编辑: 这是我的其他课程中的内容

public void ExecuteQuery(SqlCommand command)
    {
        con.Open();
        try
        {
            command.ExecuteNonQuery();
        }
        finally
        {
            con.Close();
        }
    }
public SqlCommand InitSqlCommand(string query)
    {
        var Sqlcommand = new SqlCommand(query, con);
        return Sqlcommand;
    }

以下是我的整个DatabaseConnection现在的样子:

public SqlCommand InitSqlCommand(string query, CommandType commandType)
    {
        var Sqlcommand = new SqlCommand(query, con);
        Sqlcommand.CommandType = commandType;
        return Sqlcommand;
    }
    public DataTable GetData(SqlCommand command)
    {
        var dataTable = new DataTable();
        var dataSet = new DataSet();
        var dataAdapter = new SqlDataAdapter { SelectCommand = command };
        dataAdapter.Fill(dataTable);
        return dataTable;
    }
    public void ExecuteQuery(SqlCommand command)
    {
        con.Open();
        try
        {
            command.ExecuteNonQuery();
        }
        finally
        {
            con.Close();
        }
    }
    public bool ReadSqlCommand(SqlCommand command)
    {
        con.Open();
        var activateCommand = command.ExecuteReader();

        if (activateCommand.Read())
        {
            con.Close();
            return true;
        }
        else
        {
            con.Close();
            return false;
        }
    }
    public bool ReadStringQuery(string command)
    {
        con.Open();
        var activateCommand = InitSqlCommand(command, CommandType.StoredProcedure).ExecuteReader();

        if (activateCommand.Read())
        {
            con.Close();
            return true;
        }
        else
        {
            con.Close();
            return false;
        }
    }

1 个答案:

答案 0 :(得分:2)

调用存储过程时,不需要使用参数指定数据类型,实际上甚至不需要使用exec命令添加参数,因为要在Parameters集合中添加参数,这就是你做错了改变它:

string command = "AddProduct";

并指定CommandType对象的SqlCommand,最好是设置参数并给出调用者选项以指定CommandType

public SqlCommand InitSqlCommand(string query,CommandType commandType)
{
    var Sqlcommand = new SqlCommand(query, con);
    Sqlcommand.CommandType = commandType;
    return Sqlcommand;
}

并称之为:

string command = "AddProduct";
var query = Connection.InitSqlCommand(command,CommandType.StoredProcedure);