DataSet和DataTable区别,ConnectionCommandDataReaderDataAdapterDataSet

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataSet和DataTable区别,ConnectionCommandDataReaderDataAdapterDataSet相关的知识,希望对你有一定的参考价值。

DataSet和DataTable区别  
 da.Fill(ds, "alias_1");                            //使用DataAdapter的Fill方法(填充),调用SELECT命令,别名       
 dataGridView8.DataSource = ds.Tables[0];

 da.Fill(dt);       //直接填充给dt
 flex.DataSource = dt;
 
 ds.Tables[0]==dt
 DateSet是数据在内存中的缓存,是保存数据的数据结构,相当于一个虚拟的数据库,他可以包含多个DateTable
 好像winform里面绑定只能绑定dataTable
 简单而言,dataset和datatable都是ADO的非连接层,dataset是一个位于内存中表示外部数据的对象,它是一个类型,该类型维护三个内部强类型集合,其中包括了datatablecollection,  dataset的tables属性能够访问包含多个datatable的datatablecollection


 










        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123");
            conn.Open();
            if (conn.State==ConnectionState.Open)
            {
                label1.Text = "数控库已经连接并打开";
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123");
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                label2.Text = "数控库已经连接并打开";
            }

            conn.Close();
            if (conn.State==ConnectionState.Closed)
            {
                label2.Text = "数控打开后又关闭了";
            }
        }


        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123");
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                label3.Text = "数控库已经连接并打开";
            }

            conn.Dispose();
            label3.Text = "conn.Dispose()";            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123");
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                label4.Text = "数控库已经连接并打开";
            }

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select count(*) from khxjd";
            cmd.CommandType = CommandType.Text;

            int i = Convert.ToInt32(cmd.ExecuteScalar());
            label4.Text = "表中有" + i.ToString() + "条记录!";

        }

        private void button5_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123");
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                label5.Text = "数控库已经连接并打开";
            }

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "update khxjd set qty=100+isnull(qty,0)";
            cmd.CommandType = CommandType.Text;

            int i = Convert.ToInt32(cmd.ExecuteNonQuery());
            label5.Text = "更新了表中" + i.ToString() + "条记录!";
        }


        //sqlDataReader,是向前的。
        //针对于一个sqlConnection,创建多个sqlDataReader,要先关闭上一个sqlDataReader,才能使用下一个sqlDataReader.

        private void button6_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123");
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                label6.Text = "数控库已经连接并打开";
            }

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select top 5 khxjdh,itm,nsn,prd_name_eng,prd_name_chs,qty from khxjd";
            cmd.CommandType = CommandType.Text;
                        
            SqlDataReader sdr= cmd.ExecuteReader();
            while (sdr.Read())
            {
                label6.Text = label6.Text   + "," + sdr[1].ToString();
            }

            label6.Text = label6.Text + "。读取完成!";


        }

        private void button7_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123");
            conn.Open();
            if (conn.State == ConnectionState.Open)
            {
                label7.Text = "数控库已经连接并打开";
            }

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select top 5 khxjdh,itm,nsn,prd_name_eng,prd_name_chs,qty from khxjd";
            cmd.CommandType = CommandType.Text;

            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.HasRows) 
            {
                label7.Text = "有数据";
            }
            else
            {
                label7.Text = "无数据";
            }

            sdr.Close(); 

        }


        //SqlDataAdapter:DataAdapter对象在DataSet与数据之间起桥梁作用
        private void button8_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("server=.;database=DB_JXC;uid=sa;pwd=PSWD123");

            //conn.Open();     //说明一下:使用 SqlDataAdapter时,只要SqlConnection有效就行,不需要 打开!!!

            if (conn.State == ConnectionState.Open)
            {
                label8.Text = "数控库已经连接并打开";
            }
            else
            {
                label8.Text = "数控库还未打开";  //ok
            }

            //SqlCommand cmd = new SqlCommand();
            //cmd.Connection = conn;            
            //SqlCommand cmd = new SqlCommand( "select top 5 khxjdh,itm,nsn,prd_name_eng,prd_name_chs,qty from khxjd",conn);           
            //SqlDataAdapter da = new SqlDataAdapter();
            //da.SelectCommand = cmd;

            //DataSet ds = new DataSet();
            // da.Fill(ds, "alias_1");                         //使用DataAdapter的Fill方法(填充),调用SELECT命令,别名
            //dataGridView8.DataSource = ds.Tables[0];

            


            SqlDataAdapter da = new SqlDataAdapter("select top 5 khxjdh,itm,nsn,prd_name_eng,prd_name_chs,qty from khxjd", conn); //创建DataAdapter数据适配器实例

            if (conn.State == ConnectionState.Open)
            {
                label8.Text = label8.Text+ "2.数控库已经连接并打开";
            }
            else
            {
                label8.Text = label8.Text + "2.数控库还未打开";  //ok
            }

            DataSet ds = new DataSet();                        //创建DataSet实例
            da.Fill(ds, "alias_1");                            //使用DataAdapter的Fill方法(填充),调用SELECT命令,别名       
            dataGridView8.DataSource = ds.Tables[0];
            
            //label8.Text = "数控查询完成";

            if (conn.State == ConnectionState.Open)
            {
                label8.Text = label8.Text + "3.数控库已经连接并打开";
            }
            else
            {
                label8.Text = label8.Text + "3.数控库还未打开";  //ok
            }

            //conn.Close();                                      //关闭数据库            
        }


// load table from db
string sql = "select * from products";
string conn = GetConnectionString();
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sql, conn);


DataTable dt = new DataTable();
da.Fill(dt);       //直接填充给dt

// bind to grid
_flex.Cols[0].Width = 20;
_flex.ShowCursor = true;
_flex.DataSource = dt;

// add calculated field
dt.Columns.Add("ValueInStock", typeof(decimal), "UnitPrice * UnitsInStock");
_flex.Cols["ValueInStock"].Format = "c";

// add unbound column
Column col = _flex.Cols.Add();
col.Name = col.Caption = "Unbound";

 

以上是关于DataSet和DataTable区别,ConnectionCommandDataReaderDataAdapterDataSet的主要内容,如果未能解决你的问题,请参考以下文章

C# DataSet与DataTable的区别和用法

DataSet和DataTable区别

DataSet和DataTable区别,ConnectionCommandDataReaderDataAdapterDataSet

如果我在 DataSet 而不是 DataTable 中检索数据,是不是有任何区别[重复]

Json与DataSet转化

DataTable是啥