C# Mysql 将行 move 到另一个表并获取单元格的最大值

标签 c# mysql row move

我有一种方法可以将行从一个表 move 到另一个表。这本身就很好用。我现在想做的是选择回调表中单元格的最高值。这是我尝试过的:

public static void MoveLead(RadGridView Gridview, RadDropDownList SegmentDropdown, string UniqueID)
{
    try
    {
        string Table = SegmentDropdown.Text;
        using (MySqlConnection cn = new MySqlConnection(Varribles.ConString))
        {
            string query = "BEGIN;  select max(UniqueID) from Callbacks;     INSERT INTO Callbacks select * from " + Table + " where UniqueID = " + UniqueID + "; DELETE FROM " + Table + " where UniqueID = " + UniqueID + "; COMMIT;";
            cn.Open();
            using (MySqlCommand cmd = new MySqlCommand(query, cn))
            {
                cmd.CommandText = query;
                cmd.ExecuteNonQuery();

                Console.WriteLine("query" + Convert.ToInt32(cmd.ExecuteScalar()));
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

提前致谢

最佳答案

您是否尝试过稍微更改一下代码?

public static void MoveLead(RadGridView Gridview, RadDropDownList SegmentDropdown, string UniqueID)
{
    try
    {
        string Table = SegmentDropdown.Text;
        using (MySqlConnection cn = new MySqlConnection(Varribles.ConString))
        {
            cn.Open();

            string query = "select max(UniqueID) from Callbacks;";
            using (MySqlCommand cmd = new MySqlCommand(query, cn))
            {
                // Notice I removed the command text, you are already setting the command text in the constructor for the MySqlCommand
                int UID = Int32.Parse(cmd.ExecuteScalar());

                Console.WriteLine("query" + UID);
            }

            query = "BEGIN;  INSERT INTO Callbacks select * from " + Table + " where UniqueID = ?UniqueID; DELETE FROM " + Table + " where UniqueID = ?UniqueID; COMMIT;";
            using (MySqlCommand cmd = new MySqlCommand(query, cn))
            {
                // Use parameters to sanitize input. There are very rare circumstances where you would want to do a direct concatenation to a query as its susceptible to sql injection
                cmd.Parameters.Add(new MySqlParameter("UniqueID", UniqueID))
                cmd.ExecuteNonQuery();
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

此外,我强烈建议您阅读有关 SQL 注入(inject)的内容。您构建查询的方式很可怕,具体取决于“Table”和“UniqueID”的来源。由于 Table 变量无法参数化,因此您需要格外小心该值的填充位置。 查看https://stackoverflow.com/a/652999/5947241 & https://dev.mysql.com/doc/connector-net/en/connector-net-programming-prepared-preparing.html

关于C# Mysql 将行 move 到另一个表并获取单元格的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44979578/

相关文章:

python - 在Python中使用sql将一行合并在一列中

c# - C# 和 VB.NET 中的接口(interface)实现

c# - 如何从 roslyn 代码分析器输出文件?

javascript - 为什么验证不会在客户端失败但在服务器端失败?

mysql - 获取未分配给给定 id 的条目

python - 使用python pymsql在mysql查询中出错

c# - 如何根据单元格值更改行背景颜色

c# - 在C#中访问带有 "no name"(空格)的目录/文件夹

mysql - 如何从下一条记录中获取列值

python - 删除 pandas dataFrame 中给定列中具有唯一元素的行。 (独特是指重复一次)