C#-在运行时选择多个按钮,并立即将值保存到数据库
我正在开发航空公司座位地图应用程序。但我不能一次更改多个按钮的值 我搜索了我的解决方案,但没有找到我想要的 我的顺序是C#-在运行时选择多个按钮,并立即将值保存到数据库,c#,sql,sql-server,winforms,C#,Sql,Sql Server,Winforms,我正在开发航空公司座位地图应用程序。但我不能一次更改多个按钮的值 我搜索了我的解决方案,但没有找到我想要的 我的顺序是 选择我想要被封锁的座位 按钮在选中时高亮显示 我将添加“保存”按钮。此按钮将所有选定的按钮值保存到数据库 就这些 以下是我的动态按钮代码: private void GenerateSeats() { const int seatSpacing = 6; const int middleRowWidth = 50;
private void GenerateSeats()
{
const int seatSpacing = 6;
const int middleRowWidth = 50;
const int seatWidth = 40;
const int seatHeight = 30;
char[] Seatletter = { 'A', 'B', 'C', 'D', 'E', 'F' };
//panel1.Width = 6 * (seatHeight + seatSpacing) + seatSpacing + middleRowWidth;
//panel1.Width = 1200;
var buttonSize = new Size(seatWidth, seatHeight);
for (int i = 0; i <= 155; i++)
{
int SeatRow = i / 6;
for (int j = 0; j <= 5; j++)
{
int thisRow = i / 6;
int thisColumn = j % 6;
int seatTop = thisRow * (seatHeight + seatSpacing);
int seatLeft = thisColumn * (seatWidth + seatSpacing);
string SeatCode = (SeatRow + 1).ToString() + Seatletter[j];
if (thisColumn >= 3) seatLeft += middleRowWidth;
con.Open();
cmd = new SqlCommand("select * from Passengers where seat = @checkseat", con);
cmd.Parameters.AddWithValue("@checkseat", SeatCode);
SqlDataReader Reader = cmd.ExecuteReader();
Button SButton = new Button();
SButton.Click += new EventHandler(SButton_Click);
SButton.Size = buttonSize;
SButton.BackgroundImageLayout = ImageLayout.Stretch;
SButton.TextAlign = ContentAlignment.MiddleRight;
SButton.Text = SeatCode;
SButton.Location = new Point(seatTop, seatLeft);
SeatMapPanel.Controls.Add(SButton);
if (Reader.Read()) //Check seats (available or not)
{
SButton.BackgroundImage = Properties.Resources.CXV_blk_j; //already reserved seats
}
else
{
SButton.BackgroundImage = Properties.Resources.CXV_def_j; //Available seats
}
con.Close();
}
}
}
private void generateSets()
{
const int seatspace=6;
const int middleRowWidth=50;
常数int seatWidth=40;
常数int seatHeight=30;
char[]Seatletter={'A','B','C','D','E','F'};
//面板1.宽度=6*(座椅右侧+座椅间距)+座椅间距+中间行宽度;
//面板1.宽度=1200;
var buttonSize=新尺寸(座位宽度、座位宽度);
对于(int i=0;i
弄到所有的空位
拿到所有指定的座位
弄到所有的空座位
把所有的座位都堵上
上述4项功能将为您提供可用座位号等信息
例如
List<int> GetAllTheAvailableSeats() //You can return anything which is suitable
{
//your logic of database
}
同样,对于其他状态
注意:-根据您的需要进行优化您的代码效率不高-当您可以在一次调用中获得所有分配的座位时,您可以执行156条SQL语句。创建156个按钮也不是非常有效-您可以将整件事情呈现为一个位图,然后检测鼠标单击。您的问题是还不清楚-什么不起作用?现在我希望生成具有阻止座位的座位。阻止座位仅限于预订。指定用户将选择阻止座位,而这些座位不可用于预订。因此我希望选择要阻止的座位。并立即将其保存到数据库。在单个查询中获取所有已预订的座位。您可以在单独的函数中执行此操作。然后,在生成面板时,将按钮设置回原位。如果可以使用wpf,则可以节省时间。此代码看起来效率低下。最好发送一个sql命令,而不是为每个座位发送一个命令。;)
List<int> GetAllTheAvailableSeats() //You can return anything which is suitable
{
//your logic of database
}
private void GenerateSeats()
{
const int seatSpacing = 6;
const int middleRowWidth = 50;
const int seatWidth = 40;
const int seatHeight = 30;
char[] Seatletter = { 'A', 'B', 'C', 'D', 'E', 'F' };
//panel1.Width = 6 * (seatHeight + seatSpacing) + seatSpacing + middleRowWidth;
//panel1.Width = 1200;
var buttonSize = new Size(seatWidth, seatHeight);
for (int i = 0; i <= 155; i++)
{
int SeatRow = i / 6;
for (int j = 0; j <= 5; j++)
{
int thisRow = i / 6;
int thisColumn = j % 6;
int seatTop = thisRow * (seatHeight + seatSpacing);
int seatLeft = thisColumn * (seatWidth + seatSpacing);
string SeatCode = (SeatRow + 1).ToString() + Seatletter[j];
if (thisColumn >= 3) seatLeft += middleRowWidth;
Button SButton = new Button();
SButton.Click += new EventHandler(SButton_Click);
SButton.Size = buttonSize;
SButton.BackgroundImageLayout = ImageLayout.Stretch;
SButton.TextAlign = ContentAlignment.MiddleRight;
SButton.Text = SeatCode;
SButton.Location = new Point(seatTop, seatLeft);
SeatMapPanel.Controls.Add(SButton);
}
}
}
public void SetSeatStatuses()
{
foreach(var seatinfo in AvailableSeats) //available seat can be int or anything
{
var button = SeatMapPanel.Controls[seatInfo] as Button;
//set background images here
}
}