收藏 分销(赏)

C#操作数据库总结.doc

上传人:二*** 文档编号:4576680 上传时间:2024-09-30 格式:DOC 页数:6 大小:27.50KB 下载积分:5 金币
下载 相关 举报
C#操作数据库总结.doc_第1页
第1页 / 共6页
本文档共6页,全文阅读请下载到手机保存,查看更方便
资源描述
. . . . 开发工具:Microsoft Visual Studio 2005 数据库:Microsoft SQL Server 2005 说明:这里建立的数据库名为Demo,有一个学生表Student,为操作方便起见,我只添加两个字段:studentnum和studentname. 一、SQL语句: --create database Demo use Demo create table   Student ( studentnum char(14) primary key, studentname varchar(30) not null ) insert into Student values('201','扬') 二、代码: 1.引入名称空间:using System.Data.SqlClient; 2.定义连接字符串,连接对象,命令对象:    private String connectionstr;    private SqlConnection connection;    private SqlCommand command; 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.操作数据库中的数据    (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)向数据库中添加数据        方法一:              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)修改数据库中的数据       方法一:              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)删除数据库中的数据        方法一:              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.Rows[0].Delete();                      sda.DeleteCommand = new SqlCommand(deletestr, connection);                      sda.Update(ds, "Student");                      MessageBox.Show("学生信息删除成功!", "提示", MessageBoxButtons.OK,                                       MessageBoxIcon.Information);                  }                  else                  {                      MessageBox.Show("此学号对应的学生不存在!", "错误",                                                           MessageBoxButtons.OK, MessageBoxIcon.Error);                                    } 6 / 6
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 通信科技 > 数据库/数据算法

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服