2

我提交查询,它没有抛出异常,它确实写入数据库,但不是我想要的。由于某种原因,除了我告诉查询的第一件事之外,它不会触及任何东西,并将其设置为 0。

这是我的代码:

    public static int UpdateTagDefinition(Dictionary<string, object> sqlQueryParams)
    {
        MySqlCommand query = new MySqlCommand();
        query.CommandText = "UPDATE `windows_dev` SET `name` = @name AND `desc_short` = @desc_short AND `desc_long` = @desc_long AND `tags` = @tags AND `image_title` = @image_title AND `images` = @images AND `release_date` = @release_date AND `update_date` = @update_date AND `is_on_sourcecontrol` = @is_on_sourcecontrol AND `sourcecontrol_type` = @sourcecontrol_type AND `sourcecontrol_uri` = @sourcecontrol_uri AND `download_uri` = @download_uri AND `project_uri` = @project_uri AND `source_uri` = @source_uri AND `is_public` = @is_public WHERE `DID` = @did;";
        query.Parameters.Clear();
        foreach(var sqlParam in sqlQueryParams)
        {
            query.Parameters.AddWithValue("@" + sqlParam.Key.ToString(), sqlParam.Value);
        }

        return Connector.RunSQLUpdateQuery(query);
    }

    public static int RunSQLUpdateQuery(MySqlCommand query)
    {
        try
        {
            MySqlConnection cnx = new MySqlConnection(ConfigurationManager.ConnectionStrings["******"].ConnectionString);

            // Connect to News Database and get recent article
            query.Connection = cnx;
            cnx.Open();
            MySqlDataReader Reader;
            Reader = query.ExecuteReader();
            cnx.Close();

            return 1;
        }
        catch
        {
            return 2;
        }
    }

还有我设置 SQL 参数的地方;

            Dictionary<string, object> sqlQueryParams = new Dictionary<string, object>();
            sqlQueryParams.Add("name", Request.Form["name"]);
            sqlQueryParams.Add("desc_short", Request.Form["desc_short"]);
            sqlQueryParams.Add("desc_long", Request.Form["desc_long"]);
            sqlQueryParams.Add("tags", TagDefinitions.TagDefinitionsToSQL(TagDefinitions.GetTagDefinitionIdentsFromList((List<string>)JsonConvert.DeserializeObject(Request.Form["tags"], typeof(List<string>)))));
            sqlQueryParams.Add("image_title", Request.Form["image_title"]);
            sqlQueryParams.Add("images", Request.Form["images"]);
            sqlQueryParams.Add("release_date", Request.Form["release_date"]);
            sqlQueryParams.Add("update_date", Request.Form["update_date"]);
            sqlQueryParams.Add("is_on_sourcecontrol", Request.Form["is_on_sourcecontrol"]);
            sqlQueryParams.Add("sourcecontrol_type", Request.Form["sourcecontrol_type"]);
            sqlQueryParams.Add("sourcecontrol_uri", Request.Form["sourcecontrol_uri"]);
            sqlQueryParams.Add("download_uri", Request.Form["download_uri"]);
            sqlQueryParams.Add("project_uri", Request.Form["project_uri"]);
            sqlQueryParams.Add("source_uri", Request.Form["source_uri"]);
            sqlQueryParams.Add("is_public", Request.Form["is_public"]);
            sqlQueryParams.Add("did", int.Parse(Request.Form["did"]));
4

1 回答 1

8

当您要更新多个列时,请使用COMMA而不是AND

query.CommandText = @"UPDATE `windows_dev` 
                      SET    `name` = @name, 
                             `desc_short` = @desc_short, 
                             ... other columns .....
                             `is_public` = @is_public 
                      WHERE `DID` = @did"; 
于 2012-12-25T12:46:36.363 回答