none
vb.net 2010 datagridview 問題 RRS feed

  • 問題

  • 請問各位大大:

         如何將下列從資料庫讀到的資料塞到Datagridview

    Public recset1, recset2, recset3, recset4, recset5, recset6 As New ADODB.Recordset
    Public OLEDBconn As OleDb.OleDbConnection

    Public SQLcmd, TSQLcmd As SqlCommand, Sqlconn, TSQLconn As SqlConnection, TSQLreader As SqlDataReader

    Sqlconn = New SqlConnection("Data Source=" & DBIP & ";Initial Catalog=" & DBName & ";Persist Security Info=True;" & _
                                                          "User ID=" & DBUsr & ";Password=" & DBPwd & ";Connect Timeout=120")

    OLEDBconn = New OleDb.OleDbConnection("Data Source=" & DBIP & ";Initial Catalog=" & _
                                   DBName & ";Persist Security Info=True;User ID='" & DBUsr & "';Password=" & DBPwd & _
                                   ";Provider=SQLOLEDB;Connect Timeout=120")
    SQLcmd = New SqlCommand("", Sqlconn)

    If Sqlconn.State <> ConnectionState.Open Then Sqlconn.Open()

            
            recset1.ActiveConnection = Nothing
            conn1.Open("Data Source=" & DBIP & ";Initial Catalog=" & _
                           DBName & ";Persist Security Info=True;User ID='" & DBUsr & "';Password=" & DBPwd & _
                           ";Provider=SQLOLEDB;Connect Timeout=120")

            recset1.CursorType = ADODB.CursorTypeEnum.adOpenDynamic
            recset1.LockType = ADODB.LockTypeEnum.adLockOptimistic

            recset1.Open("TableName", conn1, , ADODB.LockTypeEnum.adLockOptimistic)  'TableName是資料庫表單名稱


            If SQLconn2.State = ConnectionState.Open Then SQLconn2.Close()
            SQLconn2.Open()
            SQLcmd2.CommandText = "select 數量, 單價 from TableName where 報價單號 = '20130300001'"
            SQLreader2 = SQLcmd2.ExecuteReader(CommandBehavior.SingleResult)

            DataGridView1.DataSource =   '問題是如何把TableName 的資料塞到 DataGridView1

            SQLreader2.Close()
            SQLconn2.Close()

    感謝各為大大

    2013年3月28日 上午 06:17

解答

所有回覆