连接到 SQL

发布于 2024-09-09 23:07:35 字数 120 浏览 12 评论 0原文

我想添加一个到工作表的连接,以便每次刷新单元格时,都会从 SQL 传入一个值。我希望它引用其他工作表之一上的文本框,其中 ID =

如何设置连接以使用该值刷新。我应该在连接字符串中放入什么?我可以让它静态工作。

I want to add a connection to a sheet so that each time a cell is refreshed, a value is passed in from SQL. I want it to reference a textbox on one of the other sheets for where ID =

How do I set the connection to refresh with that value. What do I put in my connection string? I can get it to work staticly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小ぇ时光︴ 2024-09-16 23:07:35

Excel 有一个方便的小功能,称为“导入外部数据”。这是我要做的:

  1. 在 Excel 文件中,转到数据->导入外部数据->新数据库查询 (在您的情况下,“导入数据”选项不是最好的方法)

  2. 完成向导。选择您的数据源等。除最后一个选项外,请选择在 Microsoft Query 中查看数据或编辑查询

  3. MS Query 基本上是一个小型查询设计器。选择显示/隐藏条件图标(看起来像眼镜+漏斗)。

  4. 这个用于输入条件的界面很像 Access。选择一个字段,而不是硬编码值,而是使用[Criteria](或其他内容)。

  5. 关闭 MS Query 并返回到 Excel 文件。

  6. 在新数据范围中,右键单击参数。->从以下单元格获取值

  7. 不要忘记单元格值更改时自动刷新

总而言之,您正在使用参数导入外部数据,并将该参数的值声明为文件中的单元格。嗯,这是我工作之余的一次愉快的休息。我希望这有帮助。

Excel has a handy little feature called "Import External Data". Here's what I would do:

  1. In your Excel file, goto Data->Import External Data->New Database Query (In your case, the Import Data option won't be the best method)

  2. Go through the wizard. Choose your datasource, etc. Except on the last option, choose View data or edit query in Microsoft Query

  3. MS Query is basically a little query designer. Choose the show/hide criteria icon (looks like eyeglass + funnel).

  4. This interface for putting in criteria is much like Access. Choose a field and instead of hard coding values, use [Criteria] (or something).

  5. Close out MS Query and go back to the Excel file.

  6. In your new data range, right-click to Parameters.->Get the value from the following cell

  7. Don't forget the Refresh automatically when cell value changes

To summarize, you are importing external data using a parameter, and declaring the value of that parameter to be a cell in your file. Well, that was a nice little break from my own work. I hope this helps.

荒岛晴空 2024-09-16 23:07:35

是什么导致细胞被刷新?连接字符串取决于您使用的数据库。下面的示例使用 Access 并需要引用 Microsoft ActiveX Dataobjects 2.8

数据放置在sheet1 上的单元格(1,1) 中。 where 子句取自sheet2 上的textbox1:

Private Sub CommandButton1_Click() 

Dim sqlQuery As String

sqlQuery = "SELECT * FROM myTable WHERE " & Worksheets("sheet2").TextBox1.Text
fetchData "C:\file_databases\myDatabase.accdb", Worksheets("sheet1").Cells(1, 1), sqlQuery

End Sub

Private Function fetchData(databaseName As String, targetRange As Range, sqlQuery As String)

Dim connection As New ADODB.connection
Dim records As New ADODB.Recordset
Dim connectionString As String
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & databaseName & ";"

connection.Open connectionString
records.Open sqlQuery, connection
targetRange.CopyFromRecordset records

records.Close
connection.Close

End Function

What causes the cell to be refreshed? The connection string will be dependent on what database you are using. The below example uses Access and needs a reference to Microsoft ActiveX Dataobjects 2.8

The data is put in cell(1,1) on sheet1. The where clause is taken from textbox1 on sheet2:

Private Sub CommandButton1_Click() 

Dim sqlQuery As String

sqlQuery = "SELECT * FROM myTable WHERE " & Worksheets("sheet2").TextBox1.Text
fetchData "C:\file_databases\myDatabase.accdb", Worksheets("sheet1").Cells(1, 1), sqlQuery

End Sub

Private Function fetchData(databaseName As String, targetRange As Range, sqlQuery As String)

Dim connection As New ADODB.connection
Dim records As New ADODB.Recordset
Dim connectionString As String
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & databaseName & ";"

connection.Open connectionString
records.Open sqlQuery, connection
targetRange.CopyFromRecordset records

records.Close
connection.Close

End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文