aaa !連接數(shù)據(jù)庫的步驟: 1、建立SqlConnection對象; 2、指定SqlConnection對象的ConnectionString 屬性; 3、打開數(shù)據(jù)庫連接; 4、指定SQL語句; 5、建立SqlDataAdapter對象和DataSet對象(myDataAdapter = new SqlDataAdapter(strCmd,conn); ); 6、添沖DataSet; 7、給DataGrid指定數(shù)據(jù)源
SqlConnection conn = new SqlConnection(); conn.ConnectionString = "data source = (local);initial catalog = test; user id = sa;password = sa"; conn.Open(); string strCmd; strCmd = "select * from test"; SqlDataAdapter myDataAdapter; DataSet myDataSet = new DataSet(); myDataAdapter = new SqlDataAdapter(strCmd,conn); myDataAdapter.Fill(myDataSet,"test"); myTestDataGrid.DataSource = myDataSet.Tables["test"];
說明: 數(shù)據(jù)庫安裝在本地(local);數(shù)據(jù)庫名稱為test;用戶名為sa;密碼為sa; 數(shù)據(jù)庫中的表為test;
bbb!
|