首先,对不起我的英语:)
我想从数据库自动制作按钮。在数据库中,每个按钮都拥有一个ID,我会调用此ID。
我的问题很简单,如果缺少其中一个ID(如1,2,4,5),程序会在第二次读取后停止。 这是代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace e_res
{
public partial class Layout : Form
{
public Layout()
{
InitializeComponent();
}
private void Layout_Load(object sender, EventArgs e)
{
SQLFunctions Lgn = new SQLFunctions();
Lgn.ConnectionToday();
SqlCommand cmd = new SqlCommand();
cmd.Connection = SQLFunctions.conn;
int NumOfButtons = 40;
int i = 1;
cmd.CommandText = "SELECT id FROM Buttons where id='" + i + "'";
int _bId = Int32.Parse(cmd.ExecuteScalar().ToString());
// int counter = 0;
while ( _bId <= NumOfButtons)
{
if (_bId != null)
{
Button btn = new Button();
{
btn.Tag = _bId;
btn.Dock = DockStyle.Fill;
btn.Margin = new Padding(10, 10, 10, 10);
cmd.CommandText = "SELECT bName FROM Buttons where id='" + btn.Tag + "'";
btn.Text = cmd.ExecuteScalar().ToString();
string btn_name = cmd.ExecuteScalar().ToString();
btn.Name = btn_name.ToString();
/* btn.Click += delegate
{
pass_txt.Clear();
username_txt.Text = btn_name;
username_lbl.Text = btn_name;
username_lbl.Visible = true;
pass_txt.ReadOnly = false;
};*/
}
cmd.CommandText = "SELECT col FROM Buttons where id='" + btn.Tag + "'";
int btn_col = Int32.Parse(cmd.ExecuteScalar().ToString());
// MessageBox.Show(btn_col.ToString());
cmd.CommandText = "SELECT row FROM Buttons where id='" + btn.Tag + "'";
int btn_row = Int32.Parse(cmd.ExecuteScalar().ToString());
// MessageBox.Show(btn_row.ToString());
tableLayoutPanel4.Controls.Add(btn, btn_col, btn_row);
_bId++;
}
else
{
_bId++;
}
}
SQLFunctions.conn.Close();
}
private void button2_Click(object sender, EventArgs e)
{
NewButton nw = new NewButton();
nw.Show();
}
}
}
由于
答案 0 :(得分:1)
这里有一些你真正需要关注的事情,我从你的代码中将它从你的新编程中拿走,因为你最好的方法。
int _bId = Int32.Parse(cmd.ExecuteScalar().ToString());
这很危险int NumOfButtons = 40; int i = 1; cmd.CommandText =“SELECT id FROM Buttons where id ='”+ i +“'”;
您不知道数据库中有多少按钮,您无法对该数字进行硬编码。 而是做这样的事情 “SELECT id,bName,col,row FROM Buttons;”
答案 1 :(得分:0)
if (_bId != null)
{
// Here.. You need to check if _bId (For ex: 3) exists in database(by making a query), and then proceed with creating button which is rest of your code.
}
注意:您不需要在for循环中反复查询,您可以使用下面的方法一次检索所有数据。
SELECT bName,col,row FROM Buttons where id ='“+ bId +”'
如果此查询重新生成一行, //继续执行其余代码,创建Button 其他 出价++;
答案 2 :(得分:0)
将ExecuteReader()与选择所有按钮的查询一起使用。我假设MS Sql Sever支持TOP()
cmd.CommandText = "SELECT TOP(" + NumOfButtons + ") id, bname,col,row FROM Buttons ORDER BY id";
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
// process the button
}
答案 3 :(得分:0)
如果我们正在跳过“你的方法不是很有效”的部分,而只是试图使这段代码正常工作,那么你应该修改它看起来像这样:
SQLFunctions Lgn = new SQLFunctions();
Lgn.ConnectionToday();
SqlCommand cmd = new SqlCommand();
cmd.Connection = SQLFunctions.conn;
int NumOfButtons = 40;
int i = 1;
// int counter = 0;
while ( i <= NumOfButtons)
{
cmd.CommandText = "SELECT id FROM Buttons where id='" + i + "'";
int _bId = Int32.Parse(cmd.ExecuteScalar().ToString());
Button btn = new Button();
{
btn.Tag = _bId;
btn.Dock = DockStyle.Fill;
btn.Margin = new Padding(10, 10, 10, 10);
cmd.CommandText = "SELECT bName FROM Buttons where id='" + btn.Tag + "'";
btn.Text = cmd.ExecuteScalar().ToString();
string btn_name = cmd.ExecuteScalar().ToString();
btn.Name = btn_name.ToString();
/* btn.Click += delegate
{
pass_txt.Clear();
username_txt.Text = btn_name;
username_lbl.Text = btn_name;
username_lbl.Visible = true;
pass_txt.ReadOnly = false;
};*/
}
cmd.CommandText = "SELECT col FROM Buttons where id='" + btn.Tag + "'";
int btn_col = Int32.Parse(cmd.ExecuteScalar().ToString());
// MessageBox.Show(btn_col.ToString());
cmd.CommandText = "SELECT row FROM Buttons where id='" + btn.Tag + "'";
int btn_row = Int32.Parse(cmd.ExecuteScalar().ToString());
// MessageBox.Show(btn_row.ToString());
tableLayoutPanel4.Controls.Add(btn, btn_col, btn_row);
i++;
}
SQLFunctions.conn.Close();
但是如果我们谈论代码我真的会用来解决你的情况,我会将Id
的列类型更改为int,并使用1个简单的SQL命令从排序的表中获取第一个NumOfButtons
Id
。这是一个简单的SQL请求,它可以工作:
SELECT id, col, row ORDER BY id FETCH NEXT (@NumOfButtons) ROWS ONLY
我会用这个结构来运行它:
using (var reader = cmd.ExecuteReader($"SELECT id, col, row ORDER BY id FETCH NEXT ({NumOfButtons}) ROWS ONLY"))
{
while (reader.Read())
{
var id = reader.GetInt32(0);
var col = reader.GetString(1);
var row = reader.GetString(2);
// ToDo: Your stuff here
}
}
答案 4 :(得分:0)
你知道序列是什么,即它是连续的,也就是说它将从一个数字开始并且预计每次增加1?如果是这种情况,那么我可以给你一些伪代码来解决你的问题。我没有可视化工作室来检查所有代码。
然后编写一个linq语句,查找序列中的最低ID。 然后编写一个linq语句,查找序列中的最高ID。
然后使用
int last = lowest;
for (int i = lowest; i <= highest; i++)
{
int curr = (from int btn in collection/datatable where btn.ID == i select btn.ID).first;
if (curr == (last + 1))
{
curr = i;
}
else
{
++last
curr = last;
}
//make the button based on curr or not as needed
++last;
}
}