无连接模式:可以在没有打开连接时在内存中操作数据,DataAdapter通过管理连接为无连接模式提供服务,当要从数据库中查询数据时,DataAdapter打开一个连接,填充指定的DataSet,等数据读取完马上自动关闭连接,然后可以对数据做修改,再次使用DataAdapter打开连接,持久化修改(无论是更新,删除或是更新),最后自动关闭连接,使用无连接模式的情况是有一些独立数据,它们不会发生改变或者很少改变,因为在将填充DataSet和更新数据这段时间内数据库中的实际数据也许会发生改变,如果需要将数据立即持久化到数据库请使用连接模式

读取数据到DataSet:无连接意味着一个连接建立了一个与数据库的会话,请求的数据读入到DataSet中,然后通过断开数据库的连接关闭会话,这时会话因为与数据库的断开而关闭,DataSet成为一个无连接的数据库

复制代码 代码如下:
/// <summary>
      /// 查询学生信息
      /// </summary>
      /// <returns>返回填充了学生表的DataSet</returns>
      public DataSet GetUserInfor()
      {
          string str = "Data Source=.;Initial Catalog=Student;Integrated Security=True";
          var conn = new SqlConnection(str);
          DataSet ds = new DataSet();
          var sda = new SqlDataAdapter("SELECT * FROM Student",conn);
          sda.Fill(ds, "student");//调用fill方法时,SqlDataAdapter会自动打开连接,读取数据然后关闭连接
          foreach (DataRow dr in ds.Tables["student"].Rows)
          {
              Console.WriteLine(dr["name"]);
          }
          return ds;
      }

将DataSet的修改保存到数据库
插入数据

复制代码 代码如下:
/// <summary>
      /// 插入学生信息,并返回插入后的DataSet
      /// </summary>
      /// <param name="stu">学生实体类</param>
      public DataSet InsertStudnt(Student stu)
      {
          DataSet ds = GetUserInfor();
          string str = "Data Source=.;Initial Catalog=Student;Integrated Security=True";
          string sql = "INSERT INTO student VALUES (@name,@age)";
          var conn = new SqlConnection(str);
          var cmd = new SqlCommand(sql,conn);
          var sda = new SqlDataAdapter();
          SqlParameter sqlParam1 = new SqlParameter()
          {
              ParameterName = "@name",
              SourceColumn = "name"
          };
          SqlParameter sqlParam2 = new SqlParameter()
          {
              ParameterName = "@age",
              SourceColumn = "age"
          };
          SqlParameter[] sqlParamArray = new SqlParameter[] {sqlParam1,sqlParam2 };
          cmd.Parameters.AddRange(sqlParamArray);
          sda.InsertCommand = cmd;
          DataRow dr = ds.Tables["student"].NewRow();
          dr["name"] = stu.name;
          dr["age"] = stu.age;
          ds.Tables["student"].Rows.Add(dr);
          sda.Update(ds,"student");
          return ds;

      }

更新数据

复制代码 代码如下:
/// <summary>
      /// 根据ID更新姓名和年龄
      /// </summary>
      /// <param name="name">姓名</param>
      /// <param name="age">年龄</param>
      /// <param name="id">学生ID</param>
      /// <returns>返回更新后的DataSet</returns>
      public DataSet UpdateStudent(Student stu,int id)
      {
          DataSet ds = GetUserInfor();
          string str = "Data Source=.;Initial Catalog=Student;Integrated Security=True";
          string sql = "UPDATE student SET name=@name,age=@age WHERE id=@id";
          var conn = new SqlConnection(str);
          var cmd = new SqlCommand(sql, conn);
          var sda = new SqlDataAdapter();
          SqlParameter param1 = new SqlParameter()
          {
              ParameterName="@name",SourceColumn="name"
          };
          SqlParameter param2 = new SqlParameter()
          {
              ParameterName = "@age",
              SourceColumn = "age",
              SqlDbType=SqlDbType.Int
          };
          SqlParameter param3 = new SqlParameter()
          {
              ParameterName = "@id",
              SourceColumn = "id"
          };
          SqlParameter[] param = new SqlParameter[] {param1,param2,param3 };
          cmd.Parameters.AddRange(param);
          sda.UpdateCommand = cmd;
          DataTable dt = ds.Tables["student"];
          foreach (DataRow dr in dt.Rows)
          {
              int oldID=Convert.ToInt32(dr["id"]);
              if (oldID == id)
              {
                  dr["name"] = stu.name;
                  dr["age"] = stu.age;
              }
          }
          sda.Update(ds,"student");
          return ds;
      }

删除数据

复制代码 代码如下:
/// <summary>
      /// 根据ID删除一个学生
      /// </summary>
      /// <param name="id">返回更新后的DataSet</param>
      public DataSet DeleteStudent(int id)
      {
          DataSet ds = GetUserInfor();
          string str = "Data Source=.;Initial Catalog=Student;Integrated Security=True";
          string sql = "DELETE FROM student WHERE id=@id";
          var conn = new SqlConnection(str);
          var cmd = new SqlCommand(sql, conn);
          var sda = new SqlDataAdapter();
          SqlParameter param = new SqlParameter()
          {
              ParameterName="@id",SourceColumn="id",SqlDbType=SqlDbType.Int
          };
          cmd.Parameters.Add(param);
          sda.DeleteCommand = cmd;
          DataTable dt=ds.Tables["student"];
          foreach (DataRow dr in dt.Rows)
          {
              int oldId = Convert.ToInt32(dr["id"]);
              if (oldId == id)
                  dr.Delete();
          }
          sda.Update(ds,"student");
          return ds;
      }

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!