gpt4 book ai didi

mysql - 从 EXCEL 连接到 SQL 数据库

转载 作者:行者123 更新时间:2023-11-29 19:25:47 28 4
gpt4 key购买 nike

我已经完成了一些教程,但我的连接一直失败,我尝试了很多不同的连接方式。

我通过 mySQL 工作台连接到 mySQL。我使用 IP 地址和端口号,然后使用我的凭据进行登录。这运作良好,我能够执行我需要的查询。

我现在尝试通过 Excel(最好是通过 VBA)访问此数据库。我尝试创建一个新的连接,但我所做的一切似乎都不起作用。我不知道要在 strConn 字符串中放入什么。

我目前正在使用:

Options Explicit
Private Sub CommandButton2_Click()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strConn As String

Set cn = New ADODB.Connection
strConn = "DRIVER={MySQL ODBC 5.3.7 Driver};" & _
"SERVER=XXX.XXX.X.X;" & _
"PORT=3306" & _
"DATABASE=cahier_de_lab;" & _
"UID=xxx;" & _
"PWD=xxx;" & _
"Option=3"

cn.Open strConn

' Find out if the attempt to connect worked.
If cn.State = adStateOpen Then
MsgBox "Welcome to Pubs!"
Else
MsgBox "Sorry. No Pubs today."
End If

' Close the connection.
cn.Close

End Sub

感谢您的帮助!

最佳答案

从 Excel 导出到 SQL Server。

Sub InsertInto()

'Declare some variables
Dim cnn As adodb.Connection
Dim cmd As adodb.Command
Dim strSQL As String

'Create a new Connection object
Set cnn = New adodb.Connection

'Set the connection string
cnn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Northwind;Data Source=Excel-PC\SQLEXPRESS"
'cnn.ConnectionString = "DRIVER=SQL Server;SERVER=Excel-PC\SQLEXPRESS;DATABASE=Northwind;Trusted_Connection=Yes"


'Create a new Command object
Set cmd = New adodb.Command

'Open the Connection to the database
cnn.Open

'Associate the command with the connection
cmd.ActiveConnection = cnn

'Tell the Command we are giving it a bit of SQL to run, not a stored procedure
cmd.CommandType = adCmdText

'Create the SQL
strSQL = "UPDATE TBL SET JOIN_DT = '2013-01-22' WHERE EMPID = 2"

'Pass the SQL to the Command object
cmd.CommandText = strSQL


'Execute the bit of SQL to update the database
cmd.Execute

'Close the connection again
cnn.Close

'Remove the objects
Set cmd = Nothing
Set cnn = Nothing

End Sub

或者。 。 。 .

Import from SQL Server into Excel . . . . .


Sub Create_Connectionstring()

Dim objDL As MSDASC.DataLinks
Dim cnt As ADODB.Connection
Dim stConnect As String 'Instantiate the objects.

Set objDL = New MSDASC.DataLinks
Set cnt = New ADODB.Connection

On Error GoTo Error_Handling 'Show the Data-link wizard
stConnect = objDL.PromptNew 'Test the connection.
cnt.Open stConnect 'Print the string to the VBE Immediate Window.
Debug.Print stConnect 'Release the objects from memory.
exitHere:
cnt.Close
Set cnt = Nothing
Set objDL = Nothing
Exit Sub

Error_Handling: 'If the user cancel the operation.
If Err.Number = 91 Then
Resume exitHere
End If
End Sub

关于mysql - 从 EXCEL 连接到 SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42169185/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com