using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Experiment
{
public partial class frmDataBase : Form
{
public frmDataBase()
{
InitializeComponent();
}
//1)创建连接对象
SqlConnection conn = new SqlConnection();
//2)创建命令对象
SqlCommand cmd = new SqlCommand();
//数据库连接字符串赋值:请根据实际环境生成数据库连接字符串
string str = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Experiment61\Student.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True";
private void Form1_Load(object sender, EventArgs e)
{
conn.ConnectionString = str;
conn.Open();
cmd.Connection = conn;
}
private void menuBrowsing_Click(object sender, EventArgs e)//查看
{
//cmd.ExecuteReader获取数据集
dgvStudentInfo.Visible = true;
//3)给命令对象CommandText属性赋值
cmd.CommandText = "select * from student";//选择表的所有字段
//4)命令执行
SqlDataReader result = cmd.ExecuteReader(); //获取数据集
//*将记录集装载到DataTable,以此作为DataGridView的数据源
DataTable dt = new DataTable();
dt.Load(result);
dgvStudentInfo.DataSource = dt;
dgvStudentInfo.Refresh();
result.Close(); //关闭数据集
}
private void menuInsert_Click(object sender, EventArgs e) //添加
{
//根据窗体输入内容,向数据库添加一条数据,
//暂不考虑主键重复的问题
cmd.CommandText = @"Insert Into student Values('" + txtNo .Text + "','"
+txtName .Text + "','"
+ txtSex .Text + "','"
+ txtNative .Text + "','"
+ txtBirth .Text + "',"
+ txtGrade .Text + ",'"
+ txtMajor .Text + "')";
try
{
int val = cmd.ExecuteNonQuery(); //执行CommandText所要求的插入记录操作
if (val > 0)
MessageBox.Show("已在表中成功插入指定记录!", "记录插入操作");
menuBrowsing_Click(sender, e);
TextBoxClear();
}
catch (Exception ex) { MessageBox.Show(ex.ToString(), "记录插入失败!"); }
}
private void menuEdit_Click(object sender, EventArgs e) //修改
{
//根据窗体输入内容,更新数据库中对应学号的数据,即更新条件为学号=选中记录的学号。
cmd.CommandText = @"Update student Set Name='" + txtName .Text + "',"
+ "Sex='" + txtSex .Text + "',"
+ "Native='" + txtNative .Text + "',"
+ "Birth='" + txtBirth .Text + "',"
+ "Grade=" + txtGrade .Text + ","
+ "Major='" + txtMajor .Text + "'"
+ "Where No='"+txtNo .Text + "'" ;
try
{
int val = cmd.ExecuteNonQuery(); //执行CommandText所要求的修改记录操作
if (val > 0)
{
MessageBox.Show("表中指定记录修改成功!", "记录修改操作");
menuBrowsing_Click(sender, e);
TextBoxClear();
}
else
MessageBox.Show("未在表中找到要修改的记录!", "记录修改失败");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "更新数据失败!");
}
}
private void menuDelete_Click(object sender, EventArgs e)//删除
{
//将选中数据从数据库删除,即删除条件为学号=选中记录的学号。
cmd.CommandText = "delete from student where No='"+txtNo .Text+"'";
try
{
int val = cmd.ExecuteNonQuery(); //执行CommandText所要求的删除记录操作
if (val > 0)
{
MessageBox.Show("已在表中成功删除指定记录!", "记录删除操作");
menuBrowsing_Click(sender, e);
TextBoxClear();
}
else
MessageBox.Show("表中未找到要删除的记录", "记录删除操作");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "删除数据失败!");
}
}
private void TextBoxClear()
{
txtNo.Text = "";
txtName.Text = "";
txtSex.Text = "";
txtNative.Text = "";
txtBirth.Text = "";
txtGrade.Text = "";
txtMajor.Text = "";
}
private void dgvStudentInfo_CellClick(object sender, DataGridViewCellEventArgs e)
{
int i = e.RowIndex;
txtNo.Text = dgvStudentInfo.Rows[i].Cells[0].Value.ToString();
txtName.Text = dgvStudentInfo.Rows[i].Cells[1].Value.ToString();
txtSex.Text = dgvStudentInfo.Rows[i].Cells[2].Value.ToString();
txtNative.Text = dgvStudentInfo.Rows[i].Cells[3].Value.ToString();
txtBirth.Text = dgvStudentInfo.Rows[i].Cells[4].Value.ToString();
txtGrade.Text = dgvStudentInfo.Rows[i].Cells[5].Value.ToString();
txtMajor.Text = dgvStudentInfo.Rows[i].Cells[6].Value.ToString();
}
}
}