




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#操作数据库总结开发工具:Microsoft Visual Studio 2008数据库:Microsoft SQL Server 2008/ 建立数据库 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(20041000010201,张扬);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=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 str = Select * from Student Where studentnum= + snum + ; command.Connection = connection; command.CommandText = str; connection.Open(); /数据库连接打开 if (command.ExecuteScalar() = null) MessageBox.Show(你输入的学号对应的学生不在!, 错误, MessageBoxButtons.OK, MessageBoxIcon.Error); else /查询数据库中的数据 方法一(代码简洁:优) SqlDataReader sdr = command.ExecuteReader(); /sdr打开 while (sdr.Read() tBstudentnum.Text = sdrstudentnum.ToString(); tBstudentname.Text = sdrstudentname.ToString(); sdr.Close(); /sdr关闭 connection.Close(); /数据库连接关闭 private void btnQuery2_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; command.CommandText = str; connection.Open(); /数据库连接打开 if (command.ExecuteScalar() = null) MessageBox.Show(您输入的学号对应的学生不存在!, 错误, MessageBoxButtons.OK, MessageBoxIcon.Error); else /查询数据库中的数据 方法二 SqlDataAdapter sda = new SqlDataAdapter(str, connection); DataSet ds = new DataSet(); sda.Fill(ds, Student); DataTable dt = ds.TablesStudent; tBstudentnum.Text = dt.Rows0studentnum.ToString(); tBstudentname.Text = dt.Rows0studentname.ToString(); connection.Close(); /数据库连接关闭 private void btnAdd1_Click(object 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(学生学号或姓名不能为空!, 错误, MessageBoxButtons.OK, MessageBoxIcon.Error); else /向数据库中添加数据 方法一(代码简洁:优) string insertstr = insert into Student values( + snum + , + sname + ); command.Connection = connection; command.CommandText = insertstr; connection.Open(); /数据库连接打开 command.ExecuteNonQuery(); MessageBox.Show(学生添加成功!, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); connection.Close(); /数据库连接关闭 private void btnAdd2_Click(object sender, EventArgs e) SqlConnection connection = new SqlConnection(constr); string snum = tBstudentnum.Text.Trim(); string sname = tBstudentname.Text.Trim(); if (string.IsNullOrEmpty(snum) | string.IsNullOrEmpty(sname) MessageBox.Show(学生学号或姓名不能为空!, 错误, MessageBoxButtons.OK, MessageBoxIcon.Error); else /向数据库中添加数据 方法二 string str = select * from Student; string insertstr = insert into Student values( + snum + , + sname + ); SqlDataAdapter sda = new SqlDataAdapter(str, connection); DataSet ds = new DataSet(); sda.Fill(ds, Student); DataTable dt = ds.TablesStudent; DataRow dr = dt.NewRow(); drstudentnum = snum; drstudentname = 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 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(学生学号或姓名不能为空!, 错误, MessageBoxButtons.OK, MessageBoxIcon.Error); else /修改数据库中的数据 方法一(代码简洁:优) string modifystr = update Student set studentname= + sname + where studentnum= + snum + ; command.Connection = connection; command.CommandText = modifystr; connection.Open(); /数据库连接打开 command.ExecuteNonQuery(); MessageBox.Show(学生的姓名修改成功!, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); connection.Close(); /数据库连接关闭 private void btnChg2_Click(object sender, EventArgs e) SqlConnection connection = new SqlConnection(constr); string snum = tBstudentnum.Text.Trim(); string sname = tBstudentname.Text.Trim(); if (snum = | sname = ) MessageBox.Show(学生学号或姓名不能为空!, 错误, MessageBoxButtons.OK, MessageBoxIcon.Error); else /修改数据库中的数据 方法二 string str = select * from Student where 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, Student); DataTable dt = ds.TablesStudent; dt.Rows0studentname = sname; sda.UpdateCommand = new SqlCommand(updatestr, connection); sda.Update(ds, Student); MessageBox.Show(学生姓名修改成功!, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); 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.OK, MessageBoxIcon.Error); else /删除数据库中的数据 方法一(代码简洁:优) string str = select * from Student where studentnum= + snum + ; string deletestr = delete from Student where studentnum= + snum + ; command.Connection = connection; command.CommandText = str; connection.Open(); /数据库连接打开 if (command.ExecuteScalar() = null) MessageBox.Show(此学号对应的学生不存在!, 错误, MessageBoxButtons.OK, MessageBoxIcon.Error); else command.CommandText = deletestr; command.ExecuteNonQuery(); MessageBox.Show(学生的信息删除成功!, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); connection.Close(); /数据库连接关闭 private void btnDel2_Click(object sender, EventArgs e) SqlConnection connection = new SqlConnection(constr); string snum = tBstudentnum.Text.Trim(); if (s
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 营销数据驱动决策-洞察及研究
- 植物抗逆性微生物互作-洞察及研究
- 科学可视化在教育中的应用-洞察及研究
- 实时故障检测与隔离-洞察及研究
- 1.2 化合反应和分解反应教学设计-2023-2024学年华东师大版九年级上册科学
- 扎来普隆与RNA结合机制的研究与应用-洞察及研究
- 网络安全防护体系-第7篇-洞察及研究
- 元数据加密存储方案-洞察及研究
- 法治国家的构建与实践-洞察及研究
- 林分结构遥感制图-洞察及研究
- 中国食物成分表2018年(标准版)第6版
- 疑问句(课件)六年下册英语人教PEP版
- 介绍家乡恩施
- 视力残疾康复服务规范
- 【宜家家居物流运作问题与优化建议探析11000字(论文)】
- HG T 3690-2022 工业用钢骨架聚乙烯塑料复合管
- 财务报表分析方法与技巧
- 医院医保科绩效考核标准
- 《直播营销与运营》PPT商品选择与规划
- 贵阳区域分析
- 机电设备调试协议书
评论
0/150
提交评论