Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mysql请求中出现错误_C#_Mysql_Sql - Fatal编程技术网

C# mysql请求中出现错误

C# mysql请求中出现错误,c#,mysql,sql,C#,Mysql,Sql,我得到以下错误:变量名'@Title'已经声明。变量名在查询批处理或存储过程中必须是唯一的 public static string PostArticle(Article article) { try { conn.Open(); string query = "INSERT INTO Article VALUES (@Title, @UserId, @Category, @Type, @Conte

我得到以下错误:变量名'@Title'已经声明。变量名在查询批处理或存储过程中必须是唯一的

    public static string PostArticle(Article article)
    {
        try
        {
            conn.Open();
            string query = "INSERT INTO Article VALUES (@Title, @UserId, @Category, @Type, @Contents, @Thumbnail)";
            command.CommandText = query;
            command.Parameters.AddWithValue("@Title", article.Title);
            command.Parameters.AddWithValue("@UserId", article.UserId);
            command.Parameters.AddWithValue("@Category", article.Category);
            command.Parameters.AddWithValue("@Type", article.Type);
            command.Parameters.AddWithValue("@Contents", article.Contents);
            command.Parameters.AddWithValue("@Thumbnail", article.Thumbnail);
            command.ExecuteNonQuery();
            return "Post Successful";
        }
        finally 
        {
            conn.Close();
        }
    }

您可能正在尝试重新使用已有
@Title
参数的现有
命令
?从与
CreateCommand
的连接中获取新的
命令
,将参数值应用于该命令,然后执行此新命令

大概是这样的:

using (var cmd = conn.CreateCommand()) {
    cmd.CommandText = "INSERT INTO ...";
    cmd.Parameters.AddWithValue("@Title", title);
    // ... etc.
    cmd.ExecuteNonQuery();
}

它现在正在工作,我应该在末尾添加CMD.Multual.Cube(),因为这个代码不只是用于OnCEI,所以您应该考虑每次实际创建一个新的命令。或者找一个更好的方法来重构它,因为你似乎在重用一个命令,但却完全替换了它的commandtext和参数列表,这是愚蠢的。