Thứ Sáu, 27 tháng 12, 2013

3_ Thao tác trên cơ sở dữ liệu sử dụng dataset,dataadapter,datatable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;





namespace _4_Dataset_datatable_dataadapter
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionstring = "Data Source=PC1;Initial Catalog=demo1;Integrated Security=True";
            SqlConnection con = new SqlConnection(connectionstring);
            //Tao 1 dataadapter
            SqlDataAdapter ad = new SqlDataAdapter("select * from sinhvien",con);
            //Tao 1 dataset
            DataSet ds = new DataSet();
            //Do du lieu tu ad va ds dung phuong thuc fill
            ad.Fill(ds, "SinhVien");//La ten ban, co the dat tuy y
            
            //tao 1 datatable
            DataTable dt = ds.Tables["SinhVien"];
            //Duyet cac hang cac cot va in ra
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    Console.WriteLine(dt.Rows[i][j].ToString());
                }
            }
            Console.Read();
        }
    }
}

Không có nhận xét nào:

Đăng nhận xét