您好这是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();
感谢您的时间。
答案 0 :(得分:1)
我的方式:
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();
}
答案 1 :(得分:0)
为什么不给每个label
一个名字?
<强> Eidt 强>:
假设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);
.....
.....
}