slqDataReader 问题.

ericstock 2007-10-18 07:52:19
程序块一:
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
string sql = "Select Tickets.TicketId as 电影票编号,Films.FilmName as 片名,Halls.HallName as 放映厅名,Scene.SceneId as 场次编号," +
"TicketType.TypeName as 票类型名,TicketType.Discount as 票折扣率,Seats.SeatName as 座位类名," +
"Seats.BaseNum as 座位基数,Tickets.RowNumber as 座位排号,Tickets.SeatNumber as 座位座号,ScenceCost.price as 票价,Tickets.State as 状态 " +
"from ScenceCost,Films,TicketType,Halls,Seats,Scene,Tickets " +
"where ScenceCost.FilmId=Films.FilmId and " +
"ScenceCost.HallId=Halls.HallId and " +
"ScenceCost.SceneId=Scene.SceneId and " +
"ScenceCost.TicketTypeId=TicketType.TypeId and " +
"ScenceCost.SeatTypeId=Seats.SeatTypeId and " +
"ScenceCost.CostId=Tickets.CostId and " +
"Films.FilmName='" + combFilmName.Text + "' and " +
"Halls.HallName='" + combHall.Text + "' and " +
"Scene.SceneId='" + combScene.Text + "'" +
"order by Scene.SceneId desc,Halls.HallName desc,Tickets.RowNumber asc,Tickets.SeatNumber asc,TicketType.TypeName desc";
if (combFilmName.Text == "" || combHall.Text == "" || combScene.Text == "")
{
MessageBox.Show("请将数据填写完整。\n若下拉菜单里无数据,表示没有相关信息。");
return;
}
trytwice();
SqlDataAdapter adp = new SqlDataAdapter(sql, con);
DataTable table = new DataTable();
adp.Fill(table);
DGVTicketInfo.DataSource = table;
cmd.CommandText = sql;
SqlDataReader reader = cmd.ExecuteReader();
Button[] seat = new Button[100000];//定义座位
string seattype = "";//座位类名
int seatnum = new int();
int row = new int();
int col = new int();
string state;//记录状态
//以下是座位的坐标
int x = new int();
x = 10;
int y = new int();
y = 30;
int width = new int();
width = 40;
int height = new int();
height = 40;//控件的坐标和大小
while (reader.Read())
{
if (row == 0 && col == 0)
{
row = int.Parse(reader["座位排号"].ToString());
col = int.Parse(reader["座位座号"].ToString());
state = reader["状态"].ToString();
seat[seatnum] = new Button();
this.panel1.Controls.Add(seat[seatnum]);
seattype = reader["座位类名"].ToString();
if (seattype == "贵宾座位")
{
seat[seatnum].BackColor = Color.Yellow;
}
string str1 = row.ToString();
string str2 = col.ToString();
zero(ref str1, ref str2);//将button上的按钮加0
seat[seatnum].Text = str1 + "排" + str2 + "座";
seat[seatnum].SetBounds(x, y, width, height);
if (state == "未售")
{
seat[seatnum].Click += new EventHandler(seat_Click);
}
else if (state != "未售")
{
seat[seatnum].Enabled = false;
}
seatnum++;
}
else
{
if (int.Parse(reader["座位排号"].ToString()) == row && int.Parse(reader["座位座号"].ToString()) == col)
{
continue;
}
else
{
if (int.Parse(reader["座位座号"].ToString()) > col)
{
row = int.Parse(reader["座位排号"].ToString());
col = int.Parse(reader["座位座号"].ToString());
state = reader["状态"].ToString();
seat[seatnum] = new Button();
this.panel1.Controls.Add(seat[seatnum]);
seattype = reader["座位类名"].ToString();
if (seattype == "贵宾座位")
{
seat[seatnum].BackColor = Color.Yellow;
}
string str1 = row.ToString();
string str2 = col.ToString();
zero(ref str1, ref str2);
seat[seatnum].Text = str1 + "排" + str2 + "座";
x = x + width;
seat[seatnum].SetBounds(x, y, width, height);
if (state == "未售")
{
seat[seatnum].Click += new EventHandler(seat_Click);
}
else if (state != "未售")
{
seat[seatnum].Enabled = false;
}
seatnum++;
}
else
{
row = int.Parse(reader["座位排号"].ToString());
col = int.Parse(reader["座位座号"].ToString());
state = reader["状态"].ToString();
seat[seatnum] = new Button();
this.panel1.Controls.Add(seat[seatnum]);
seattype = reader["座位类名"].ToString();
if (seattype == "贵宾座位")
{
seat[seatnum].BackColor = Color.Yellow;
}
string str1 = row.ToString();
string str2 = col.ToString();
zero(ref str1, ref str2);
seat[seatnum].Text = str1 + "排" + str2 + "座";
if (col == 1)
{
x = 10;
y = y + height;
}
else
{
x = x + width;
}
seat[seatnum].SetBounds(x, y, width, height);
if (state == "未售")
{
seat[seatnum].Click += new EventHandler(seat_Click);
}
else if (state != "未售")
{
seat[seatnum].Enabled = false;
}
seatnum++;
}
}
}
}
con.Close();
if (seatnum == 0)
{
MessageBox.Show("sorry,没有您所要的票,购买票务失败!");
reader.Close();
return;
}
}
我按第一次按button1时候没有问题
第二次按button1时 出现错误
提示为:
ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。

这是为什么??
怎么该?
...全文
123 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ec8483 2007-10-19
  • 打赏
  • 举报
回复
每个子程序都用上
con.Open();
con.close();
那样就不会有那种错误的吧
whgames_007 2007-10-19
  • 打赏
  • 举报
回复
数据库在哪里打开的啊!!是不是trytwice();不像啊


还有提点建议,数据库不用出现中文字段!!
wqy1981 2007-10-19
  • 打赏
  • 举报
回复
ben_bear 2007-10-18
  • 打赏
  • 举报
回复
con.Open();
SDFDSAC 2007-10-18
  • 打赏
  • 举报
回复
是不是别的地方把连接关了?
ericstock 2007-10-18
  • 打赏
  • 举报
回复
trytwice();
是将sqlconnection con 打开。
ericstock 2007-10-18
  • 打赏
  • 举报
回复
SqlDataReader reader = cmd.ExecuteReader(); 这句话出错了
提示为:
ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。
是为什么??

684

社区成员

发帖
与我相关
我的任务
社区描述
智能路由器通常具有独立的操作系统,包括OpenWRT、eCos、VxWorks等,可以由用户自行安装各种应用,实现网络和设备的智能化管理。
linuxpython 技术论坛(原bbs)
社区管理员
  • 智能路由器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧