C#操作数据库小结.doc_第1页
C#操作数据库小结.doc_第2页
C#操作数据库小结.doc_第3页
C#操作数据库小结.doc_第4页
C#操作数据库小结.doc_第5页
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论