數據庫連接查詢 二 、

還是不是很理解DATASET再寫了一個 ,開始是在控制檯裏面執行的。 感覺顯示數據不是很方便後來又把那裏面的代碼直接拷貝的一個WINDOWS窗體的程序,感覺還好。拉了兩個BUTTON空件,和一個 dataGridView的空件。

      private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Data.SqlClient.SqlConnection sqlconnection;
                System.Data.SqlClient.SqlCommand sqlcommand;
                System.Data.SqlClient.SqlDataAdapter da;
                sqlconnection = new System.Data.SqlClient.SqlConnection();
                sqlconnection.ConnectionString = "Data Source=(local);Initial Catalog=Northwind;User ID=sa";
                sqlcommand = new System.Data.SqlClient.SqlCommand();
                sqlcommand.Connection = sqlconnection;
                sqlcommand.CommandText = "select * from customers ";
                da = new SqlDataAdapter(sqlcommand.CommandText, sqlconnection);
                sqlconnection.Open();
                DataSet ds = new DataSet();
                da.Fill(ds, "orders");
                da.SelectCommand.CommandText = "select * from employees";
                da.Fill(ds, "employees");
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    Console.WriteLine("table=" + i.ToString() + "/n" + "name=" + ds.Tables[i].TableName);
                }

                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = null;
        } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章