1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| Public Enum MyAdo
ACCESS97
ACCESS2000
ACCESS2012
ODBC
ORACLE
SQLSERVER2005
SQLSERVER2018
SQLite
SQLite3
CSV
xls
MySQL
End Enum
Sub Test()
Dim CnServeur As Object, CnExcel As Object: Set CnServeur = CreateObject("AdoDb.Connection"): Set CnExcel = CreateObject("AdoDb.Connection")
CnServeur.Open = GenereCSTRING(SQLSERVER2018, "TblCAB", "MOI", "MOI", "DESKTOP-NQG9COU\SQLEXPRESS")
CnExcel.Open GenereCSTRING(xls, ThisWorkbook.FullName)
Range("A:C").ClearContents
sql = "IF OBJECT_ID('tempdb..#MyTblCAB') IS NOT NULL DROP TABLE #MyTblCAB" & vbCrLf & _
"CREATE TABLE #MyTblCAB (CAB varchar(30))" & vbCrLf
With CnExcel.Execute("Select [CAB] From [TblCAB$]")
If Not .EOF Then
t = "('" & .GetString(, , "", "'),('", "")
t = Left(t, Len(t) - 3)
sql = sql & "INSERT INTO #MyTblCAB VALUES " & vbCrLf
sql = sql & t & vbCrLf
End If
.Close
End With
CnServeur.Execute sql
'ActiveCell.CopyFromRecordset CnServeur.Execute("Select * from #MyTblCAB")
Debug.Print sql
sql = "[Customer].dbo.MAJ_TableCAB" ' je nomme ma procédure stockée MAJ_TableCAB
With CreateObject("ADODB.Command")
.ActiveConnection = CnServeur
.CommandText = sql
.CommandTimeout = 0 'evite que le delai d'attente de requete expire (que la requte bugue)
.CommandType = 4 'invoque une procédure stockée. Le serveur SQL doit savoir où aller chercher la commande, c'est le pourquoi de la ligne qui spécifie le CommandType
.Parameters.Append .CreateParameter("@nom", 200, 1, 129, "AA") 'CAB
Debug.Print sql
Set rs = .Execute
'ActiveCell.CopyFromRecordset rs
End With
With CreateObject("ADODB.Command")
.ActiveConnection = CnServeur
.CommandText = sql
.CommandTimeout = 0 'evite que le delai d'attente de requete expire (que la requte bugue)
.CommandType = 4 'invoque une procédure stockée. Le serveur SQL doit savoir où aller chercher la commande, c'est le pourquoi de la ligne qui spécifie le CommandType
.Parameters.Append .CreateParameter("@nom", 200, 1, 129, "BB") 'CAB
' ActiveCell.CopyFromRecordset .Execute
End With
With ThisWorkbook.Sheets("test")
For i = 0 To rs.Fields.Count - 1 'On place le nom des champs sur la ligne 4 de ma feuille Excel
.Range("A1").Offset(0, i) = rs(i).Name
Next
.Range("A2").CopyFromRecordset rs 'la on copie le résultat de la requête
End With
End Sub
Private Function GenereCSTRING(TYPEBASE As MyAdo, Base As String, Optional User As String, Optional PassWord As String, Optional Server As String, Optional AvecTitre As Boolean = True)
'Permet de générer le Cornec String
'1 - ACCESS 97
'2 - ACCESS 2000
'3 - ACCESS 2012
'4 - ODBC
'5 - ORACLE
'6 - SQL SERVER 2005
'7 - SQL Server 2008 R2
'8 - SQLite
'9 - SQLite3
If Trim("" & Base) = "" Then Base = Base
Select Case TYPEBASE
Case xls
GenereCSTRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Base & ";Extended Properties=""Excel 12.0;HDR=" & Array("No", "YES")(Abs(AvecTitre)) & ";"""
Case ACCESS97
GenereCSTRING = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" & Base
Case ACCESS2000
GenereCSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Base & ";Persist Security Info=False"
Case ACCESS2012
GenereCSTRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Base & ";"
Case MySQL
GenereCSTRING = " DRIVER={MySQL ODBC 5.1 Driver};SERVER=" & Server & ";UID=" & User & ";DATABASE=" & Base & ";Password=" & PassWord
Case ODBC
GenereCSTRING = "Provider=MSDASQL.1;Password=" & PassWord & ";Persist Security Info=True;User ID=" & User & ";Data Source=" & Base
Case ORACLE
GenereCSTRING = "Provider=OraOLEDB.Oracle.1;Password=" & PassWord & ";Persist Security Info=True;User ID=" & User & ";Data Source=" & Base
Case SQLSERVER2005
'GenereCSTRING = "Driver={SQL Server};SERVER=" & Server & ";DATABASE=" & Base & ";UID=" & User & ";Pwd=" & PassWord & ";"
GenereCSTRING = "Provider=SQLOLEDB.1;Password=" & PassWord & ";Persist Security Info=True;User ID=" & User & ";Initial Catalog=" & Base & ";Data Source=" & Server
Case SQLSERVER2018
GenereCSTRING = "Provider=SQLOLEDB.1;Password=" & "exploitation" & ";Persist Security Info=True;User ID=" & "MdP" & ";Initial Catalog=" & "Archive" & ";Data Source=" & "00.00.00.00" & " " '* SQLServer2018
Case SQLite
GenereCSTRING = "Provider=OleSQLite.SQLiteSource.3; Data Source=" & Base
GenereCSTRING = "Driver={SQLite ODBC (UTF-8) Driver};Database=" & Base & ";StepAPI=;Timeout="
Case SQLite3
GenereCSTRING = "Driver={SQLite3 ODBC Driver};Database=" & Base & ";LongNames=0;Timeout=4000;NoTXN=0;SyncPragma=NORMAL;StepAPI=0;"
Case CSV
GenereCSTRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Server & ";Extended Properties=""Text;HDR=" & Array("No", "YES")(Abs(AvecTitre)) & ";FMT=Delimited;"""
Case Else
GenereCSTRING = "PAS ASSEZ DE PARAMETRES RENSEIGNES !!!"
End Select
End Function |
Partager