




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 正态分布曲线下面积分布规律王万荣90课件
- 职业康复职业培训山东医学高等专科学校康复医学教研室93课件
- 水电基本知识培训课件
- 二零二五年度房屋租赁押金退还与赔偿协议
- 2025版拆除工程安全监理合同-重点措施与施工安全培训记录
- 二零二五年度网络安全防护与应急响应服务合同
- 2025版绿化工程苗木运输及栽种合同
- 二零二五年度合同管理部门合同管理标准化与规范化合同
- 二零二五年旅游车辆租赁与景区旅游咨询服务合同
- 二零二五年度建筑工程施工安全文明施工合同模板文件
- 洗车场专项施工方案
- YY/T 1766.3-2023X射线计算机体层摄影设备图像质量评价方法第3部分:双能量成像与能谱应用性能评价
- 中药饮片采购配送服务投标方案
- 风光电站网络信息系统安全事故应急演练方案
- 五大神电力华煤炭公司劳动定员统一标准
- WB/T 1036-2006菱镁制品用玻璃纤维布
- 【词汇】高中英语新教材词汇总表(共七册)
- 北京市各县区乡镇行政村村庄村名明细
- 笔迹、指纹鉴定申请书
- 长沙市历年中考数学试卷,2014-2021年长沙中考数学近八年真题汇总(含答案解析)
- 【英语】人教版英语八年级英语下册阅读理解专题复习练习(含解析)
评论
0/150
提交评论