连到SQL数据库

时间:2022-06-20 05:51:47
                     string _sql;
                     SqlConnection cn;
                     _sql="server=localhost;uid=sa;pwd=hz0222;database=Northwind";
                     cn=new SqlConnection (_sql);
                     cn.Open ();
                     //SqlCommand cmd =new SqlCommand ("select * from Products ",cn);
                     //SqlDataReader rs;
                     //rs=cmd.ExecuteReader();
                     SqlDataAdapter da=new SqlDataAdapter ("select * from Products",cn);
 
                     DataSet ds=new DataSet ();
                     da.Fill (ds,"Products");
 
                     DataGrid1.DataSource =ds.Tables["Products"].DefaultView ;
                     //DataGrid1.CurrentPageIndex = 0;
                     DataGrid1.DataBind ();
                     cn.Close ();