这是用于将数据从数据库显示到gridview的代码....如果我在GridView中进行任何更改并且必须在数据库中更新...我不知道该怎么做...任何人都可以帮助我....
Dim DBCONSRT, QRYSTR As String
Dim strSQL As String
Dim DBCON, myConn, myCommand, rs As Object
Dim NoOfRecords As Long
rs = CreateObject("ADODB.Recordset")
Const DB_CONNECT_STRING = "Provider=MSDASQL.1;Persist Security Info=False;User ID=cpa5k;Data Source=NP1;DSN=NP1;UID=cpa5k;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
strSQL = "select * from QA1MM.STRSK_OH FETCH FIRST 10 ROWS ONLY with ur;"
rs.Open(strSQL, myConn)
Dim myDA As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim myDS As DataSet = New DataSet
myDA.Fill(myDS, rs, "MyTable")
DataGridView1.DataSource = myDS.Tables(0)
DataGridView1.Refresh()
myConn.Close()
我正在尝试这种方法。但它无法正常工作
Dim myDA As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
Dim myDS As DataSet = New DataSet
myDA.Fill(myDS, rs, "MyTable")
ds = DataGridView1.DataSource
myDA.Update(ds)
我改变了这样:
Dim DBCONSRT, QRYSTR As String
Dim strSQL As String
Dim DBCON, myConn, myCommand, rs As Object
Dim ds As DataSet = New DataSet
rs = CreateObject("ADODB.Recordset")
Const DB_CONNECT_STRING = "Provider=MSDASQL.1;Persist Security Info=False;User ID=cpa5k;Data Source=NP1;DSN=NP1;UID=cpa5k;PASSWORD=mexico13;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
Dim myDA As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter
Dim myDS As DataSet = New DataSet
Dim dtable As DataTable = New DataTable()
myDA.UpdateCommand = New SqlClient.SqlCommand("UPDATE QA1MM.STRSK_OH set OH_QTY = 10 WHERE SKU_NBR = 108011", myConn)
' myDA.UpdateCommand.Parameters.Add("@OH_QTY", OleDb.OleDbType.VarChar, 15, "OH_QTY")
' myDA.UpdateCommand.Parameters.Add("@SKU_NBR", OleDb.OleDbType.VarChar, 15, "SKU_NBR")
' myDA.UpdateCommand.Parameters(0).SourceVersion = DataRowVersion.Current
' myDA.UpdateCommand.Parameters(1).SourceVersion = DataRowVersion.Current
dtable = DataGridView1.DataSource
myDA.Update(dtable)
myConn.Close()
但它给出的错误如“无法将类型'System .__ ComObject'的COM对象强制转换为类类型'System.Data.SqlClient.SqlConnection'。表示COM组件的类型实例无法转换为不具有类型的类型的实例表示COM组件;但只要底层COM组件支持对接口的IID的QueryInterface调用,它们就可以转换为接口。
我更改了连接字符串,如:
Provider=MSDASQL.1;Persist Security Info=False;Data
Source=NP1;DSN=NP1;SDSN=Default;HST=ibslnpb1.sysplex.homedepot.com;PRT=4101;Integrated
Security = True;Initial Catalog=QA1MM;
它不工作。错误是“多步OLE DB操作生成错误。检查每个OLE DB状态值,如果可用。没有工作。”.....抱歉,我不知道如何更改.. ...
答案 0 :(得分:2)
你写的就像跟着
myConn.Open(DB_CONNECT_STRING)
myDA.Update(ds.Tables(0))
myConn.Close()
编辑:
不要将DataGridView1的数据源放入数据集中,而是按照以下方式进入DataTable
Dim dtable As New DataTable()
dtable = DataGridView1.DataSource
并使用DataAdapter将表更新为
myConn.Open(DB_CONNECT_STRING)
myDA.Update(dtable)
myConn.Close()
答案 1 :(得分:0)
试试这个:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DataSet1.Tables("employee").AcceptChanges()
Dim i As Integer
Dim cmdbuilder As New OdbcCommandBuilder(odbcadptr)
i = odbcadptr.Update(odbcds, "customer")
MsgBox("Updated Rows: " & i)
End Sub