//加载窗口 private void A_returnBook_Load(object sender, EventArgs e) { SqlConnection conn = SQLbase.getConn(); try { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Open(); SqlCommand selectCmd = new SqlCommand("select * from returntable", conn); SqlDataReader datareader = selectCmd.ExecuteReader(); string RID, BID, Bname; int rowindex = 0; this.datagridview.Rows.Clear(); while (datareader.Read()) { RID = datareader["RID"].ToString(); Bname = datareader["Bname"].ToString(); BID = datareader["BID"].ToString(); rowindex = this.datagridview.Rows.Add(); //trim函数同于去除字符串两头的空格 this.datagridview.Rows[rowindex].Cells[0].Value = RID.Trim(); this.datagridview.Rows[rowindex].Cells[1].Value = BID.Trim(); this.datagridview.Rows[rowindex].Cells[2].Value = Bname.Trim(); } } catch { MessageBox.Show("操作错误!"); } }
//查询按钮 private void button1_Click(object sender, EventArgs e) { SqlConnection conn = SQLbase.getConn(); try { this.datagridview.Rows.Clear(); //清空表格 //打开连接 if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Open(); string selectStr = "select * from Borrow"; //按读者号查询 if (comboBox_QueryStyle.SelectedIndex == 0) { string rid = txt_query.Text.ToString(); //建立查询命令 selectStr = "select * from Borrow where RID=" + rid; } //按书号查询 else if (comboBox_QueryStyle.SelectedIndex == 1) { string bid = txt_query.Text.ToString(); //建立查询命令 selectStr = "select * from Borrow where BID=" + bid; } //没有选择下拉框,则查询全部 SqlCommand selectCmd = new SqlCommand(selectStr, conn); SqlDataReader datareader = selectCmd.ExecuteReader(); string BID, Bname, RID, Rname, BorrowDate, RealDate, Note; int rowindex = 0; this.datagridview.Rows.Clear(); while (datareader.Read()) { RID = datareader["RID"].ToString(); BID = datareader["BID"].ToString(); BorrowDate = datareader["BorrowDate"].ToString(); RealDate = datareader["RealDate"].ToString(); Note = datareader["Note"].ToString(); rowindex = this.datagridview.Rows.Add(); //trim函数同于去除字符串两头的空格 this.datagridview.Rows[rowindex].Cells[0].Value = BID.Trim(); this.datagridview.Rows[rowindex].Cells[2].Value = RID.Trim(); this.datagridview.Rows[rowindex].Cells[4].Value = BorrowDate.Trim(); this.datagridview.Rows[rowindex].Cells[5].Value = RealDate.Trim(); this.datagridview.Rows[rowindex].Cells[6].Value = Note.Trim(); } datareader.Close(); SQLbase.close(selectCmd); int n = this.datagridview.RowCount; for (int i = 0; i < n; i++) { //填充书名 BID = this.datagridview.Rows[i].Cells[0].Value.ToString(); Bname = selectBook(conn, BID); this.datagridview.Rows[i].Cells[1].Value = Bname.Trim(); //填充作者名 RID = this.datagridview.Rows[i].Cells[2].Value.ToString(); Rname = selectReader(conn, RID); this.datagridview.Rows[i].Cells[3].Value = Rname.Trim(); } } catch { MessageBox.Show("查无此记录!"); } SQLbase.close(conn); }