//加载窗口函数 private void R_historyRecord_Load(object sender, EventArgs e) { SqlConnection conn = SQLbase.getConn(); try { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Open(); string RID = LogWindow.logname; SqlCommand selectCmd = new SqlCommand("select BID,BorrowDate,ReturnDate,RealDate,Note from Borrow where RID='" + RID + "'", conn); SqlDataReader datareader = selectCmd.ExecuteReader(); string Bid, Bname, BorrowDate, ReturnDate, RealDate, Note; int rowindex = 0; this.dataGridView1.Rows.Clear(); while (datareader.Read()) { Bid = datareader["BID"].ToString(); BorrowDate = datareader["BorrowDate"].ToString(); ReturnDate = datareader["ReturnDate"].ToString(); RealDate = datareader["RealDate"].ToString(); Note = datareader["Note"].ToString(); rowindex = this.dataGridView1.Rows.Add(); //trim函数同于去除字符串两头的空格 this.dataGridView1.Rows[rowindex].Cells[0].Value = Bid.Trim(); this.dataGridView1.Rows[rowindex].Cells[2].Value = BorrowDate.Trim(); this.dataGridView1.Rows[rowindex].Cells[3].Value = ReturnDate.Trim(); this.dataGridView1.Rows[rowindex].Cells[4].Value = RealDate.Trim(); this.dataGridView1.Rows[rowindex].Cells[5].Value = Note.Trim(); } datareader.Close(); SQLbase.close(selectCmd); int n = this.dataGridView1.RowCount; for (int i = 0; i < n; i++) { Bid = this.dataGridView1.Rows[i].Cells[0].Value.ToString(); Bname = select(conn, Bid); this.dataGridView1.Rows[i].Cells[1].Value = Bname.Trim(); } } catch { MessageBox.Show("操作错误!"); } SQLbase.close(conn); }
//查询按钮 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); }