zoukankan      html  css  js  c++  java
  • ADO.NET连接方式

    使用Command、DataReader和DataSet两种方法实现数据绑定

         方法1:使用Command和DataReader 

     

    SqlConnection con = new SqlConnection("server=.;database=Department;uid=sa;pwd=123456");
                con.Open();
                string sqlStr = "select * from emp"; 
                SqlCommand  sqlCmd = new SqlCommand(sqlStr, con);
                SqlDataReader  reader = sqlCmd.ExecuteReader();
                GridView1.DataSource = reader;
                GridView1.DataBind();
    


          方法2:使用DataSet数据集(DataAdapter可以无需打开(con.Open()方法),可以自己实现)


                SqlDataAdapter sda = new SqlDataAdapter();
                SqlConnection con = DB.createCon();
                SqlCommand cmd = new SqlCommand();
                string sqlStr = "select * from emp";
                sda.SelectCommand = new SqlCommand(sqlStr, con);
                DataSet ds = new DataSet();
                sda.Fill(ds, "employee");
                GridView1.DataSource = ds.Tables["employee"].DefaultView;
                GridView1.DataBind();
    

        Fill方法隐式执行DataAdapter的SelectCommand中的SQL查询,查询的结果用于定义DataSet表的结构,并用数据来填充表。


    SqlCommand和SqlDataAdapter的区别


        SqlCommand对应DataReader  SqlDataAdapter对应DataSet DataAdapter表示一组SQL命令和一个数据库连接,它们用于填充DataSet和更新数据源

     

        .NET中读取数据库中的数据有两种方式,一种是基于连接的方式,就象你所给出的程序那种方式,还有一种就是基于非连接的方式,就是你说的使用DataSet保存结果集。

        基于连接方式读取数据时,结果放在DataReader流中(内存流),DataReader是只向前且是只读,本代码中将DataReader的数据读出后放在一个泛型集合里,将泛型集合返回给方法的调用者,方法的调用者就可以对泛型集合进行各种操作。

        基于非连接方式读取数据时,需要将查询语句赋给DataAdapter的SelectCommand方法,通过DataAdapter的Fill方法将数据库中的数据填充到DataSet,然后DataSet就可以与数据显示控件进行绑定,进行用户交互了


     

    SQLHelper中的方法:(方法2和此方法其实一样)

     

    SqlDataAdapter是数据适配器,而SqlCommand是命令对象,SqlDataAdapterda = new SqlDataAdapter(cmd);就是执行你的SQL。

     

    CommandType.Text代表执行的是SQL语句

    CommandType.StoreProcedure代表执行的是存储过程

    CommandType代表要执行的类型


     

    Public Function ExecSelect(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTable  
      
            Dim sqlAdapter As SqlDataAdapter  
            Dim dt As New DataTable  
            Dim ds As New DataSet  
            '还是给cmd赋值  
            cmd.CommandText = cmdText  
            cmd.CommandType = cmdType  
            cmd.Connection = conn  
            cmd.Parameters.AddRange(sqlParams)  '参数添加  
            sqlAdapter = New SqlDataAdapter(cmd)  '实例化adapter  
            Try  
                sqlAdapter.Fill(ds)           '用adapter将dataSet填充   
                dt = ds.Tables(0)             'datatable为dataSet的第一个表  
                cmd.Parameters.Clear()        '清除参数  
            Catch ex As Exception  
                MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")  
            Finally                            '最后一定要销毁cmd  
                Call CloseCmd(cmd)  
            End Try  
            Return dt  
        End Function  
    



  • 相关阅读:
    python反爬之js混淆-字符串映射
    How to Provide a Default Trace for a Cloud Application
    Reset Peak Used Memory
    SAP 课程
    note 1639578 and 1622837
    SAP License error
    SAP BASIS-System Move
    初识Agile(敏捷项目管理)
    SAP HANA升级
    SAP FIORI 部署
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3170248.html
Copyright © 2011-2022 走看看