實驗目的:
學習增、刪、改、查操作。
實驗內容:
學習增刪改查操作
目標效果:
實驗步驟:
1、數據庫數據準備:
需要在數據庫裏準備一張表,以便下面可以進行使用
2、創建新項目:
3、根據目標效果從工具箱添加控件:
(1)添加”DataGridView“,並且連接數據(具體步驟參考:數據庫實驗2)
(2)添加控件:
4、設置“增”按鈕的點擊事件:
private void Insert_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuAge = textBox4.Text.Trim();
String StuSdept = textBox5.Text.Trim();
try
{
con.Open();
string insertStr = "INSERT INTO Student(Sno,Sname,Ssex,Sage,Sdept) " +
"VALUES('" + StuID + "','" + StuName + "','" + StuSex + "'," + StuAge + ",'" + StuSdept + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("輸入數據違反要求");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student);
}
注意事項:
string insertStr = "INSERT INTO Student(Sno,Sname,Ssex,Sage,Sdept) " +
"VALUES(" + StuID + "," + StuName + "," + StuSex + "," + StuAge + "," + StuSdept + ")";
這麼寫是錯誤的,等價的SQL語句爲:
INSERT INTO Student(Sno,Sname,Ssex,Sage,Sdept) VALUES(201215134,秦修,男,23,FM)
和數據庫裏定義的屬性是不相符合的,要根據屬性的要求添加單引號。
5、設置“刪”的點擊事件:
private void Delete_Click(object sender, EventArgs e)
{
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當前行第一列的值,也就是ID
string delete_by_id = "delete from Student where Sno=" + select_id;//sql刪除語句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("請正確選擇行!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student);
}
注意事項:
在進行刪除測試的時候,選擇的數據有要求。由於在我的數據庫中,有些數據和在另一張中也使用到了,這時候如果進行刪除操作,是不能實現的。
6、設置“改”的點擊事件:
private void Update_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
try
{
con.Open();
string insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("輸入數據違反要求!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student);
}
7、設置“查”的點擊事件:
private void Select_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String conn = "Data Source=.;Initial Catalog=SCHOOL;User ID=sa;Password=********";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
String select_by_id = "select * from Student where Sno='" + StuID + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id,sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查詢語句有誤!");
}
finally
{
sqlConnection.Close();
}
this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student);
}
8、設置“Close”的點擊事件
private void Close_Click(object sender, EventArgs e)
{
Application.Exit();
}
運行效果:
(1)增加數據
增加數據失敗:
(2)刪除數據
未正確選擇行時:
(3)修改數據
(4)查詢數據
代碼:
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.Threading.Tasks;
using System.Windows.Forms;
namespace CRUD
{
public partial class Form1 : Form
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=SCHOOL;User ID=sa;Password=*********");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 這行代碼將數據加載到表“sCHOOLDataSet.Student”中。您可以根據需要移動或刪除它。
this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Insert_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
String StuSex = textBox3.Text.Trim();
String StuAge = textBox4.Text.Trim();
String StuSdept = textBox5.Text.Trim();
try
{
con.Open();
string insertStr = "INSERT INTO Student(Sno,Sname,Ssex,Sage,Sdept) " +
"VALUES('" + StuID + "','" + StuName + "','" + StuSex + "'," + StuAge + ",'" + StuSdept + "')";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("輸入數據違反要求");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student);
}
private void Close_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Delete_Click(object sender, EventArgs e)
{
try
{
con.Open();
string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//選擇的當前行第一列的值,也就是ID
string delete_by_id = "delete from Student where Sno=" + select_id;//sql刪除語句
SqlCommand cmd = new SqlCommand(delete_by_id, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("請正確選擇行!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student);
}
private void Update_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String StuName = textBox2.Text.Trim();
try
{
con.Open();
string insertStr = "UPDATE Student SET Sname = '" + StuName + "' WHERE Sno = '" + StuID + "'";
SqlCommand cmd = new SqlCommand(insertStr, con);
cmd.ExecuteNonQuery();
}
catch
{
MessageBox.Show("輸入數據違反要求!");
}
finally
{
con.Dispose();
}
this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student);
}
private void Select_Click(object sender, EventArgs e)
{
String StuID = textBox1.Text.Trim();
String conn = "Data Source=.;Initial Catalog=SCHOOL;User ID=sa;Password=************";
SqlConnection sqlConnection = new SqlConnection(conn);
try
{
sqlConnection.Open();
String select_by_id = "select * from Student where Sno='" + StuID + "'";
SqlCommand sqlCommand = new SqlCommand(select_by_id,sqlConnection);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = sqlDataReader;
dataGridView1.DataSource = bindingSource;
}
catch
{
MessageBox.Show("查詢語句有誤!");
}
finally
{
sqlConnection.Close();
}
this.studentTableAdapter.Fill(this.sCHOOLDataSet.Student);
}
}
}
心得:
並不是很順利,踩了一些坑