Home > Software engineering >  How to use the OpenDataSource method, vb import XLS to SQL server2005 database
How to use the OpenDataSource method, vb import XLS to SQL server2005 database

Time:11-14

How to use the OpenDataSource method, vb import XLS to SQL server2005 database
 
Dim objConn As New ADODB. Connection
Const recordsaffected=30
Const adCmdText=1
Const adCmdTable=2
Const adOpenKeyset=1
Const adLockPessimistic=2

'to connect to the database
Public Sub ConnectToServer ()
On Error GoTo Errmsg
ObjConn. Open "driver={SQL Server}; Server="& amp; DBIP & amp; "; Uid="& amp; DBuid & amp; "; The PWD="& amp; DBpwd & amp; "; The database="& amp; DBName & amp; ";"
The Exit Sub
Errmsg:
MsgBox "the database connection error, please contact your administrator!" & Err, Description, vbInformation
End
End Sub

'here is the import program
'first remove that data, support repeatedly import
CommandText="delete from" & amp; STableName & amp; "Where pt_dt='" & amp; ProDataDate & amp; "'
"ObjConn. Execute CommandText recordsaffected, adCmdText
'the import data
CommandText="insert into" & amp; STableName & amp; "(" & amp; The Replace (sFieldNames, "|", ", ") & amp; ") "& amp; _
"SELECT" & amp; The Replace (sFieldNames, "|", ", ") & amp; _
"FROM OpenDataSource (' Microsoft. Jet. The OLEDB. 4.0", "& amp; _
"' Data Source=" "" & amp; CheckFilePath (App. Path) & amp; SFileName & amp; "" "; User ID=; Password=; Extended properties=8.0 ') Excel "& amp; _
"... [" & amp; sTableName & amp; "_" & amp; the Format (DTPicker1) Value, "YYYYMMDD") & amp; "$]"
ObjConn. Execute CommandText recordsaffected, adCmdText
Print "of" new import & amp; STableName & amp; "Table" & amp; I_n & amp; "A record of success!"

'but it is an error, VB object closing operation is not allowed I don't know why?

'want to import, fast speed, but cannot solve the above problem, finally had to use Excel. The Application, a a insert, slowly
  • Related