You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.7 KiB

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;
namespace Sql
{
public partial class Form311 : Form
{
public Form311()
{
InitializeComponent();
toolStripStatusLabel2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
timer1.Start();
Table();
}
public void Table() //此私有类是将数据加载到窗体中
{
dataGridView1.Rows.Clear(); //清除前面的数据,方便后面重新刷新数据
string sql = "select * from Book";
Dao dao = new Dao();
IDataReader dr = dao.read(sql);
while (dr.Read())
{
string a, b, c, d, e, f, g, h;
a = dr["Id"].ToString(); //将数据加入到表中
b = dr["Name"].ToString();
c = dr["Author"].ToString();
d = dr["DOP"].ToString();
e = dr["Count"].ToString();
f = dr["Publisher"].ToString();
g = dr["Category"].ToString();
h = dr["State"].ToString();
string[] str = { a, b, c, d, e, f, g, h };
dataGridView1.Rows.Add(str); //添加数据
}
dr.Close();//关闭连接
}
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form312 f = new Form312(this);
f.ShowDialog();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult r = MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.OKCancel);
if (r == DialogResult.OK)
{
string id, name;
id = dataGridView1.SelectedCells[0].Value.ToString();
name = dataGridView1.SelectedCells[1].Value.ToString();
string sql = "delete from Book where Id='" + id + "'and Name='" + name + "'";
MessageBox.Show("已删除图书——" + name + "");
Dao dao = new Dao();
dao.Execute(sql);
Table(); //调用36行的私有类函数实现刷新表的功能
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}