将多个标签的位置保存到 SQL 数据库

本文关键字:保存 SQL 数据库 位置 标签 | 更新日期: 2023-09-27 18:33:36

您好,这是creates multiple labels保存在SQL DB中的确切位置的代码。现在我想知道如何保存每个标签的位置(因为我能够像Drag and drop一样移动它们)。我想知道代码应该是什么样子,因为当我在运行时创建标签时,它没有名称,不是吗?我不确定应该如何分配它。

有人能帮我解决这个问题吗?

这是加载的代码:

private void createLabelFromSql()
        {
            try
            {
                string query = "SELECT * FROM [schema] WHERE id=@id";
                SqlCommand com = new SqlCommand(query, conn);
                com.Parameters.AddWithValue("@id", idSch);
                conn.Open();
                SqlDataReader read= com.ExecuteReader();
                while (read.Read())
                {
                    mouseX = Int32.read(read["x"].ToString());
                    mouseY = Int32.read(read["y"].ToString());
                    createLabelCmd();
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("" + ex);
            }
            finally { conn.Close(); }
        }
    private void createLabelCmd()
        {
                        newLabel = new Label();
                        newLabel.Location = new Point(mouseY, mouseX);
                        newLabel.MouseMove += new MouseEventHandler(this.MyControl_MouseMove);
                        newLabel.MouseDown += new MouseEventHandler(this.MyControl_MouseDown);
                        panel1.Controls.Add(newLabel);
        }

我以为它可能以某种方式是这样的,但不确定如何处理名称的事情:

            string query = "UPDATE [schema] SET x=@x, y=@y WHERE id=@id";
      SqlCommand cmd = new  SqlCommand(query, conn);
        cmd.Parameters.AddWithValue("@x",label1.Location.X);
        cmd.Parameters.AddWithValue("@y", label1.Location.Y);
      conn.Open();
      cmd.ExecuteNonQuery();
      conn.Close();

谢谢你的时间。

将多个标签的位置保存到 SQL 数据库

我的方式:

 private void createLabelFromSql()
    {
        try
        {
            string query = "SELECT * FROM [schema] WHERE id=@id";
            SqlCommand com = new SqlCommand(query, spojeni);
            com.Parameters.AddWithValue("@id", idSch);
            spojeni.Open();
            SqlDataReader precti = com.ExecuteReader();
            while (precti.Read())
            {
                createLabelCmd((int)precti["x"], (int)precti["y"]);
            }
            spojeni.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("" + ex);
        }
        finally { spojeni.Close(); }
    }
    private void createLabelCmd(int x, int y)
    {
        var newLabel = new Label();
        newLabel.Location = new Point(y, x);
        newLabel.Font = new Font(newLabel.Font.FontFamily.Name, 9, FontStyle.Bold);
        newLabel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
        newLabel.MouseMove += new MouseEventHandler(this.MyControl_MouseMove);
        newLabel.MouseDown += new MouseEventHandler(this.MyControl_MouseDown);
        panel1.Controls.Add(newLabel);
    }
    private void SaveAllLabels()
    {
        spojeni.Open();
        //delete all data
        SqlCommand delCmd = new SqlCommand("DELETE FROM  [schema] WHERE id=@id", spojeni);
        delCmd.Parameters.AddWithValue("@id", idSch);
        delCmd.ExecuteNonQuery();
        //create new data for current state
        string query = "INSERT INTO [schema] VALUES (@x, @y, @id)";
        SqlCommand cmd = new SqlCommand(query, spojeni);
        cmd.Parameters.Add("@x", SqlDbType.Int);
        cmd.Parameters.Add("@y", SqlDbType.Int);
        cmd.Parameters.AddWithValue("@id", idSch);
        foreach (Control item in panel1.Controls)
        {
            if (item is Label)
            {
                cmd.Parameters["@x"].Value = item.Location.X;
                cmd.Parameters["@y"].Value = item.Location.Y;
                cmd.ExecuteNonQuery();
            }
        }
        spojeni.Close();
    }

你为什么不给每个label起一个名字?

开斋节:假设id是表的Primary Key,您可以通过以下方式获取它:

private void createLabelFromSql()
{
....
....
 while (read.Read())
 {
     mouseX = Int32.read(read["x"].ToString());
     mouseY = Int32.read(read["y"].ToString());
     createLabelCmd(Int32.Parse(read["id"].ToString())); // you can extract the id here
 }
 conn.Close();
}

并将保存方法更改为:

 string query = "UPDATE [schema] SET x=@x, y=@y WHERE id=@id";
 SqlCommand cmd = new  SqlCommand(query, conn);
 cmd.Parameters.AddWithValue("@x",label1.Location.X);
 cmd.Parameters.AddWithValue("@y", label1.Location.Y);
 cmd.Parameters.AddWithValue("@id", int.Parse(label1.Name.Replace("lbl","")));
....

编辑结束 ,

private void createLabelCmd(int id)
{
    newLabel = new Label();
    newLabel.Name = "lbl" + id.ToString();
    newLabel.Location = new Point(mouseY, mouseX);
.....
.....
}