




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、C#操作數(shù)據(jù)庫總結開發(fā)工具:Microsoft Visual Studio 2008數(shù)據(jù)庫:Microsoft SQL Server 2008/ 建立數(shù)據(jù)庫 Demo,有一個學生表 Student(studentnum,studentname)/ create database Demo;/ use Demo;/ create table Student/ (/ studentnum char(14) primary key,/ studentname varchar(30) not null/ );/ insert into Student values('2004100001020
2、1','張揚');using System;using System.Data;using System.Data.SqlClient;using System.Windows.Forms;namespace Demo public partial class Form1 : Form private String constr = "server=37;uid=sa;pwd=123qwe.;database=Demo" /遠程sa賬號登錄 /private String connectionstr = "server
3、=localhost;Integrade Security=SSPI;database=Demo" 本地管理員賬號登錄 public Form1() InitializeComponent(); private void btnQuery1_Click(object sender, EventArgs e) SqlConnection connection = new SqlConnection(constr); SqlCommand command = new SqlCommand(); string snum = tBstudentnum.Text.Trim(); string
4、str = "Select * from Student Where studentnum='" + snum + "'" command.Connection = connection; command.CommandText = str; connection.Open(); /數(shù)據(jù)庫連接打開 if (command.ExecuteScalar() = null) MessageBox.Show("你輸入的學號對應的學生不在!", "錯誤", MessageBoxButtons.OK, Mess
5、ageBoxIcon.Error); else /查詢數(shù)據(jù)庫中的數(shù)據(jù) 方法一(代碼簡潔:優(yōu)) SqlDataReader sdr = command.ExecuteReader(); /sdr打開 while (sdr.Read() tBstudentnum.Text = sdr"studentnum".ToString(); tBstudentname.Text = sdr"studentname".ToString(); sdr.Close(); /sdr關閉 connection.Close(); /數(shù)據(jù)庫連接關閉 private void btn
6、Query2_Click(object sender, EventArgs e) SqlConnection connection = new SqlConnection(constr); SqlCommand command = new SqlCommand(); string snum=tBstudentnum.Text.Trim(); string str = "select * from Student where studentnum='" + snum + "'" command.Connection = connection
7、; command.CommandText = str; connection.Open(); /數(shù)據(jù)庫連接打開 if (command.ExecuteScalar() = null) MessageBox.Show("您輸入的學號對應的學生不存在!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); else /查詢數(shù)據(jù)庫中的數(shù)據(jù) 方法二 SqlDataAdapter sda = new SqlDataAdapter(str, connection); DataSet ds = new DataSe
8、t(); sda.Fill(ds, "Student"); DataTable dt = ds.Tables"Student" tBstudentnum.Text = dt.Rows0"studentnum".ToString(); tBstudentname.Text = dt.Rows0"studentname".ToString(); connection.Close(); /數(shù)據(jù)庫連接關閉 private void btnAdd1_Click(object sender, EventArgs e) SqlC
9、onnection connection = new SqlConnection(constr); SqlCommand command = new SqlCommand(); string snum = tBstudentnum.Text.Trim(); string sname = tBstudentname.Text.Trim(); if (string.IsNullOrEmpty(snum) | string.IsNullOrEmpty(sname) MessageBox.Show("學生學號或姓名不能為空!", "錯誤", MessageBox
10、Buttons.OK, MessageBoxIcon.Error); else /向數(shù)據(jù)庫中添加數(shù)據(jù) 方法一(代碼簡潔:優(yōu)) string insertstr = "insert into Student values('" + snum + "','" + sname + "')" command.Connection = connection; command.CommandText = insertstr; connection.Open(); /數(shù)據(jù)庫連接打開 command.ExecuteNo
11、nQuery(); MessageBox.Show("學生添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); connection.Close(); /數(shù)據(jù)庫連接關閉 private void btnAdd2_Click(object sender, EventArgs e) SqlConnection connection = new SqlConnection(constr); string snum = tBstudentnum.Text.Trim(); string s
12、name = tBstudentname.Text.Trim(); if (string.IsNullOrEmpty(snum) | string.IsNullOrEmpty(sname) MessageBox.Show("學生學號或姓名不能為空!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); else /向數(shù)據(jù)庫中添加數(shù)據(jù) 方法二 string str = "select * from Student" string insertstr = "insert int
13、o Student values('" + snum + "','" + sname + "')" SqlDataAdapter sda = new SqlDataAdapter(str, connection); DataSet ds = new DataSet(); sda.Fill(ds, "Student"); DataTable dt = ds.Tables"Student" DataRow dr = dt.NewRow(); dr"studentnum
14、" = snum; dr"studentname" = sname; dt.Rows.Add(dr); sda.InsertCommand = new SqlCommand(insertstr, connection); sda.Update(ds, "Student"); MessageBox.Show("學生添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); private void btnChg1_Click(object
15、 sender, EventArgs e) SqlConnection connection = new SqlConnection(constr); SqlCommand command = new SqlCommand(); string snum = tBstudentnum.Text.Trim(); string sname = tBstudentname.Text.Trim(); if (string.IsNullOrEmpty(snum) | string.IsNullOrEmpty(sname) MessageBox.Show("學生學號或姓名不能為空!",
16、"錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); else /修改數(shù)據(jù)庫中的數(shù)據(jù) 方法一(代碼簡潔:優(yōu)) string modifystr = "update Student set studentname='" + sname + "' where studentnum='" + snum + "'" command.Connection = connection; command.CommandText = modifystr;
17、 connection.Open(); /數(shù)據(jù)庫連接打開 command.ExecuteNonQuery(); MessageBox.Show("學生的姓名修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); connection.Close(); /數(shù)據(jù)庫連接關閉 private void btnChg2_Click(object sender, EventArgs e) SqlConnection connection = new SqlConnection(constr);
18、 string snum = tBstudentnum.Text.Trim(); string sname = tBstudentname.Text.Trim(); if (snum = "" | sname = "") MessageBox.Show("學生學號或姓名不能為空!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); else /修改數(shù)據(jù)庫中的數(shù)據(jù) 方法二 string str = "select * from Student where
19、 studentnum='" + snum + "'" ; string updatestr = "update Student set studentname='" + sname + "' where studentnum='" + snum + "'" SqlDataAdapter sda = new SqlDataAdapter(str, connection); DataSet ds = new DataSet(); sda.Fill(ds, &q
20、uot;Student"); DataTable dt = ds.Tables"Student" dt.Rows0"studentname" = sname; sda.UpdateCommand = new SqlCommand(updatestr, connection); sda.Update(ds, "Student"); MessageBox.Show("學生姓名修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Informa
21、tion); private void btnDel1_Click(object sender, EventArgs e) SqlConnection connection = new SqlConnection(constr); SqlCommand command = new SqlCommand(); string snum = tBstudentnum.Text.Trim(); if (string.IsNullOrEmpty(snum) MessageBox.Show("學生學號不能為空!", "錯誤", MessageBoxButtons.O
22、K, MessageBoxIcon.Error); else /刪除數(shù)據(jù)庫中的數(shù)據(jù) 方法一(代碼簡潔:優(yōu)) string str = "select * from Student where studentnum='" + snum + "'" string deletestr = "delete from Student where studentnum='" + snum + "'" command.Connection = connection; command.Command
23、Text = str; connection.Open(); /數(shù)據(jù)庫連接打開 if (command.ExecuteScalar() = null) MessageBox.Show("此學號對應的學生不存在!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error); else command.CommandText = deletestr; command.ExecuteNonQuery(); MessageBox.Show("學生的信息刪除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); connection.Close(); /數(shù)據(jù)庫連接關閉 private void btnDel2_Click(object sender, EventArgs e) SqlConnection connection = new SqlConnection(constr); string snum = tBstudentnum.Text.Trim(); if (string.IsNullOrEmpty(snum) MessageBox.Show("學生學號不能為空!",
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 無人機操控與航拍技術考核試卷
- 圖書館數(shù)字資源長期保存策略考核試卷
- 家電產(chǎn)品品質(zhì)監(jiān)控與質(zhì)量改進考核試卷
- 整年運輸合同范本
- 大板委托加工合同范本
- 修剪綠化直營合同范本
- 工地個人水電合同范本
- 小學生美術課件制作教學
- 名片合同范本
- 財務支出季度計劃工作的分解與執(zhí)行要點
- 三年級下冊 道德與法治 全冊教案
- 2024年安徽合肥中考數(shù)學試題及答案1
- GB/T 44264-2024光伏組件清潔機器人通用技術條件
- 2024-2025學年全國中學生天文知識競賽備賽試題庫(含答案)
- 2024年四川省德陽市中考英語試卷真題(含答案解析)
- 三水高樁碼頭施工組織設計方案
- 2024年九年級中考語文課外文言文閱讀題匯集(一)附答案解析
- 醫(yī)療器械的驗收與管理制度
- 部編人教版七年級下冊道德與法治全冊課件
- 護理文件書寫PDCA課件
- 2025(統(tǒng)編版)四年級上冊語文開學第一課
評論
0/150
提交評論