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();
}
}
}