C#操作数据库总结_第1页
C#操作数据库总结_第2页
C#操作数据库总结_第3页
C#操作数据库总结_第4页
C#操作数据库总结_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

开发工具 Microsoft Visual Studio 2005 数据库 Microsoft SQL Server 2005 说明 这里建立的数据库名为 Demo 有一个学生表 Student 为操作方便起见 我 只添加两个字段 studentnum 和 studentname 一 一 SQLSQL 语句语句 create database Demo use Demo create table Student studentnum char 14 primary key studentname varchar 30 not null insert into Student values 20041000010201 张扬 二 代码 二 代码 1 1 引入名称空间引入名称空间 using using System Data SqlClient System Data SqlClient 2 2 定义连接字符串定义连接字符串 连接对象 命令对象连接对象 命令对象 private String connectionstr private SqlConnection connection private SqlCommand command 3 3 在构造函数中初始化连接字符串 连接对象 命令对象在构造函数中初始化连接字符串 连接对象 命令对象 1 初始化连接字符串 方式 connectionstr server localhost uid sa pwd 123456 database Demo 方式 connectionstr server 127 0 0 1 Integrade Security SSPI database Demo 其中 SIMS 是我要连接的数据库名 1 中的 uid 和 pwd 是你登录数据库的 登录名和密码 注 这种连接是连接本地的数据库 若要连接局域网内其它机子上的数据 库 可将方式 的 server localhost 改为 server 数据库所在机子的 IP 2 初始化连接对象 connection new SqlConnection connectionstr 3 初始化命令对象 command new SqlCommand command Connection connection 4 4 操作数据库中的数据操作数据库中的数据 1 1 查询数据库中的数据查询数据库中的数据 方法一 string snum tBstudentnum Text Trim string str select from Student where studentnum snum command CommandText str connection Open if command ExecuteScalar null MessageBox Show 您输入的学号对应的学生不存在 错误 MessageBoxButtons OK MessageBoxIcon Error else SqlDataReader sdr command ExecuteReader while sdr Read tBstudentnum Text sdr studentnum ToString tBstudentname Text sdr studentname ToString sdr Close connection Close 方法二 string snum tBstudentnum Text Trim string str select from Student where studentnum snum 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 Tables Student tBstudentnum Text dt Rows 0 studentnum ToString tBstudentname Text dt Rows 0 studentname ToString connection Close 2 2 向数据库中添加数据向数据库中添加数据 方法一 string snum tBstudentnum Text Trim string sname tBstudentname Text Trim if snum sname MessageBox Show 学生学号或姓名不能为空 错误 MessageBoxButtons OK MessageBoxIcon Error else string insertstr insert into Student values snum sname command CommandText insertstr connection Open command ExecuteNonQuery MessageBox Show 学生添加成功 提示 MessageBoxButtons OK MessageBoxIcon Information connection Close 方法二 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 Tables Student DataRow dr dt NewRow dr studentnum snum dr studentname sname dt Rows Add dr sda InsertCommand new SqlCommand insertstr connection sda Update ds Student MessageBox Show 学生添加成功 提示 MessageBoxButtons OK MessageBoxIcon Information 3 3 修改数据库中的数据修改数据库中的数据 方法一 string snum tBstudentnum Text Trim string sname tBstudentname Text Trim if snum sname MessageBox Show 学生学号或姓名不能为空 错误 MessageBoxButtons OK MessageBoxIcon Error else string modifystr update Student set studentname sname where studentnum snum command CommandText modifystr connection Open command ExecuteNonQuery MessageBox Show 学生的姓名修改成功 提示 MessageBoxButtons OK MessageBoxIcon Information connection Close 方法二 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 Tables Student dt Rows 0 studentname sname sda UpdateCommand new SqlCommand updatestr connection sda Update ds Student MessageBox Show 学生姓名修改成功 提示 MessageBoxButtons OK MessageBoxIcon Information 4 4 删除数据库中的数据删除数据库中的数据 方法一 string snum tBstudentnum Text Trim if 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 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 方二 string str select from Student where studentnum snum string deletestr delete from Student where studentnum snum SqlDataAdapter sda new SqlDataAdapter str connection DataSet ds new DataSet sda Fill ds Student DataTable dt ds Tables Student if dt Rows Count 0 dt R

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论