aaaa
28-02-2003, 11:26
Cách 1 : (trong trường hợp này là cho kết quả vào DataList)
Private oConn As SqlConnection
oConn = New SqlConnection(Value)
Dim oCmd As SqlCommand
Dim strSQL As String
strSQL = "Select * from table1"
oCmd = New OleDbCommand(strSQL, oConn)
Datalist.DataSource=oCmd.ExecuteReader (return as SqlDataReader)
Datalist.DataBin()
Cái cách này thì các bước hoàn toàn rõ ràng, ban đầu tạo 1 kết nối, sau đó tạo 1 SqlCommand, cuối cùng dùng method ExecuteReader để trả về đối tượng SqlDataReader. À nhưng khi đem áp dụng đối với DataGrid thì tạch, kô thể hiểu nổi sao các bác à
Cách 2 : (đưa kết quả vào DataGrid)
CSDL có 1 bảng tên "Comments" (đây là CSDL cho 1 guestbook mà)
Private oConn As OleDbConnection
oConn = New OleDbConnection(value)
Dim myDS As New DataSet()
Dim sqlCmd As String = "SELECT * FROM Comments ORDER BY AddedDate Desc"
Dim myDAdp As OleDbDataAdapter = New OleDbDataAdapter(sqlCmd, oConn)
myDAdp.Fill(myDS, "Comments")
DataGrid.DataSource=myDS.Tables("Comments")
"
Private oConn As OleDbConnection
oConn = New OleDbConnection(value)
"
==> OK, chỗ này hiểu
"
Dim myDS As New DataSet()
Dim sqlCmd As String = "SELECT * FROM Comments ORDER BY AddedDate Desc"
Dim myDAdp As OleDbDataAdapter = New OleDbDataAdapter(sqlCmd, oConn)
myDAdp.Fill(myDS, "Comments")
"
Dataset cần DataAdapter để interact with data source==> OK, nhưng trong sqlCmd đã thể hiện cần kết sử dụng table nào và những filed nào rồi , thế còn "Comments" trong myDAdp.Fill(myDS, "Comments") để làm cái gì, chả nhẽ để biết là lấy từ table nào à, nếu sqlCmd kết nối nhiều bảng thì parameter thứ 2 trong myDAdp.Fill ở trên là gì.
Theo định nghĩa Dataset là 1 object gồm nhiều collection như Relations, table Collection, Default View DataGrid.DataSource=myDS.Tables("Comments") chắc ý rằng trong MyDS lấy Table Collection và chọn table Comments phải không.
Nhờ mọi người giải thích các thắc mắc trên nhé. Thank củi mọi người.
Private oConn As SqlConnection
oConn = New SqlConnection(Value)
Dim oCmd As SqlCommand
Dim strSQL As String
strSQL = "Select * from table1"
oCmd = New OleDbCommand(strSQL, oConn)
Datalist.DataSource=oCmd.ExecuteReader (return as SqlDataReader)
Datalist.DataBin()
Cái cách này thì các bước hoàn toàn rõ ràng, ban đầu tạo 1 kết nối, sau đó tạo 1 SqlCommand, cuối cùng dùng method ExecuteReader để trả về đối tượng SqlDataReader. À nhưng khi đem áp dụng đối với DataGrid thì tạch, kô thể hiểu nổi sao các bác à
Cách 2 : (đưa kết quả vào DataGrid)
CSDL có 1 bảng tên "Comments" (đây là CSDL cho 1 guestbook mà)
Private oConn As OleDbConnection
oConn = New OleDbConnection(value)
Dim myDS As New DataSet()
Dim sqlCmd As String = "SELECT * FROM Comments ORDER BY AddedDate Desc"
Dim myDAdp As OleDbDataAdapter = New OleDbDataAdapter(sqlCmd, oConn)
myDAdp.Fill(myDS, "Comments")
DataGrid.DataSource=myDS.Tables("Comments")
"
Private oConn As OleDbConnection
oConn = New OleDbConnection(value)
"
==> OK, chỗ này hiểu
"
Dim myDS As New DataSet()
Dim sqlCmd As String = "SELECT * FROM Comments ORDER BY AddedDate Desc"
Dim myDAdp As OleDbDataAdapter = New OleDbDataAdapter(sqlCmd, oConn)
myDAdp.Fill(myDS, "Comments")
"
Dataset cần DataAdapter để interact with data source==> OK, nhưng trong sqlCmd đã thể hiện cần kết sử dụng table nào và những filed nào rồi , thế còn "Comments" trong myDAdp.Fill(myDS, "Comments") để làm cái gì, chả nhẽ để biết là lấy từ table nào à, nếu sqlCmd kết nối nhiều bảng thì parameter thứ 2 trong myDAdp.Fill ở trên là gì.
Theo định nghĩa Dataset là 1 object gồm nhiều collection như Relations, table Collection, Default View DataGrid.DataSource=myDS.Tables("Comments") chắc ý rằng trong MyDS lấy Table Collection và chọn table Comments phải không.
Nhờ mọi người giải thích các thắc mắc trên nhé. Thank củi mọi người.