在VB中显示Gridview中的数据

时间:2012-03-28 12:05:41

标签: vb.net

我想在DataGridView中显示来自数据库的数据......这是我的代码......它无法正常工作......任何人都可以帮我扫描......

    Dim DBCONSRT, QRYSTR As String
    Dim strSQL, skunbr As String
    Dim DBCON, myConn, myCommand, rs As Object
    Dim NoOfRecords As Long
    skunbr = TextBox1.Text
    rs = CreateObject("ADODB.Recordset")
    Const DB_CONNECT_STRING = "Provider=MSDASQL.1;Persist Security Info=False;User ID=cpa5k;Data Source=NP1;DSN=NP1;UID=user;PASSWORD=pass;SDSN=Default;HST=ibslnpb1.sysplex.homedepot.com;PRT=4101;Initial Catalog=QA1MM;"
    myConn = CreateObject("ADODB.Connection")
    myCommand = CreateObject("ADODB.Command")
    myConn.Open(DB_CONNECT_STRING)
    myCommand.ActiveConnection = myConn
    myCommand.CommandText = "update QA1MM.STRSK_OH set OH_QTY = 250 where SKU_NBR = 100013 and STR_NBR = 116;"
    myCommand.Execute()
    strSQL = "select * from QA1MM.STRSK_OH where SKU_NBR =  " & skunbr & "  with ur FETCH FIRST 10 ROWS ONLY;"
    rs.Open(strSQL, myConn)
    DataGridView1.DataSource = rs
    DataGridView1.Refresh()
    myConn.Close()

2 个答案:

答案 0 :(得分:1)

不是像这样直接将ADODB.RecordSet分配给datagridview的数据源,而是首先将记录集值转换/填充到如下所示的数据集

Dim myDA As OleDbDataAdapter = New OleDbDataAdapter
Dim myDS As DataSet = New DataSet

myDA.Fill(myDS, rs, "MyTable")
DataGridView1.DataSource = myDS.Tables(0)     
DataGridView1.Refresh() 

答案 1 :(得分:0)

这是经典的VB方式。不是.net

请参阅此MSDN链接