来自 SQL Server 2005 Express 表的 VFP 组合框内容

发布于 2024-12-05 00:14:21 字数 198 浏览 0 评论 0原文

我仍然是 VFP 的新手,我正在寻找有关如何将 SQL Server 表字段拉入 VFP 组合框(或其他对象,如果更好的话)的建议和建议,就像自动完成一样,但来自 sql server 数据库。

我的表中有大约 2 列和 1000 行,组合框应该只显示第二列字段供用户选择,但使用所选的第一列字段记录到另一个表中。我希望你能明白。

预先感谢您的反馈。

I'm still newbie in VFP, I'm looking for advices and suggestions on how to pull SQL Server table fields into VFP combobox(or other objects as well, if its better), just like auto-complete but from sql server database.

I have about 2 columns and 1000 rows inside the table, the combobox should only show the second columns field for user to choose but use the chosen first column field to be recorded to another table. I hope you get the idea.

Thanks in advance for your feedback.

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

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

发布评论

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

评论(2

陌路黄昏 2024-12-12 00:14:21

我会稍微不同地处理它,因为你想要两列......一列显示,一列显示实际数据。组合框可以直接绑定到表或游标(游标只不过是一个临时表,关闭时会自动删除)。

在组合框的 INIT() 中,我将对 SQL 数据库运行查询,但只需获取这两列...

* -- Try to connect and get a connection handle.
lnHandle = Sqlstringconnect( YourConnectionString )
if lnHandle < 1
  */ Return, get out, unable to get handle to SQL server
  messagebox( "Unable to connect to SQL" )
  return
end 

lcSQLCmd = "Select ShowThisColumn, ButUseThisColumn from YourDatabase.dbo.YourTable"
lnSQLResult = SQLExec( lnHandle, lcSQLCmd, "C_ChoicesFromSQL" )
sqldisconnect( lnHandle )
if lnSQLResult < 1
  messagebox( "Unable to retrieve data" )
  return
endif

*/ Ok, we have the data, now the binding.  VFP Can set the row source directly
*/ to the ALIAS ("C_ChoicesFromSQL") from the SQL query directly, no need to scan
*/ and add items.  Just tell it both columns.
This.ColumnCount = 2   && You will bind BOTH columns to the combobox
This.BoundColumn = 2   && You want the data from the SECOND column bound for storage
This.BoundTo     = .T.  && Yes, bind to whatever the Control Source of the combobox

*/ Here's the trick.  Set the column widths = whatever you have visible on the form
*/ for the first column, and  the second column has a visible width of 0
This.ColumnWidths = alltrim( str( This.Width )) + ",0"

*/ Now, the row source... 
This.RowSource = "C_ChoicesFromSQL.ShowThisColumn, ButUseThisColumn"
This.RowSourceType = 2  && bound to alias

*/ Fixed dropdown LIST, dont allow others to type in their own values
*/ but only choose from the data available in your source.
This.Style = 2 

I would handle it a little differently since you want BOTH columns.. one shown, and one for the actual data. Comboboxes can be directly bound to a table or cursor (cursor is nothing more than a temp table that automatically gets erased when closed).

In the INIT() of your combobox, I would run your query to your SQL database, but just grab those two columns...

* -- Try to connect and get a connection handle.
lnHandle = Sqlstringconnect( YourConnectionString )
if lnHandle < 1
  */ Return, get out, unable to get handle to SQL server
  messagebox( "Unable to connect to SQL" )
  return
end 

lcSQLCmd = "Select ShowThisColumn, ButUseThisColumn from YourDatabase.dbo.YourTable"
lnSQLResult = SQLExec( lnHandle, lcSQLCmd, "C_ChoicesFromSQL" )
sqldisconnect( lnHandle )
if lnSQLResult < 1
  messagebox( "Unable to retrieve data" )
  return
endif

*/ Ok, we have the data, now the binding.  VFP Can set the row source directly
*/ to the ALIAS ("C_ChoicesFromSQL") from the SQL query directly, no need to scan
*/ and add items.  Just tell it both columns.
This.ColumnCount = 2   && You will bind BOTH columns to the combobox
This.BoundColumn = 2   && You want the data from the SECOND column bound for storage
This.BoundTo     = .T.  && Yes, bind to whatever the Control Source of the combobox

*/ Here's the trick.  Set the column widths = whatever you have visible on the form
*/ for the first column, and  the second column has a visible width of 0
This.ColumnWidths = alltrim( str( This.Width )) + ",0"

*/ Now, the row source... 
This.RowSource = "C_ChoicesFromSQL.ShowThisColumn, ButUseThisColumn"
This.RowSourceType = 2  && bound to alias

*/ Fixed dropdown LIST, dont allow others to type in their own values
*/ but only choose from the data available in your source.
This.Style = 2 
窗影残 2024-12-12 00:14:21

好吧,下面是我如何以编程方式执行此操作,假设表单上有一个名为“cboSQL”的组合框,并且此代码位于表单 Init() 方法中(这只执行组合中的一列,但请检查 AddItem() 下的 VFP 帮助) :

Local lnHandle, lnResult As Integer
Local lcConnstring, lcCommand As String
Local llReturn As Boolean

    * -- Do the default behaviour.
    llReturn = DoDefault()

    If llReturn

        * -- Try to connect to a local SQL Express 2008 instance.
        lcServername = ".\SQLEXPRESS"
        lcDbname = "umbraco"
        lcConnstring = [server=]+ lcServername+ [;driver={SQL Server};database=]+ lcDbname
        lcConnstring = lcConnstring + [;DSN='';Trusted_Connection=Yes]

        * -- Try to connect and get a connection handle.
        lnHandle = Sqlstringconnect(lcConnstring)

        * -- Got a connection ?
        If lnHandle > 0

            * -- Run a query, put the results in a VFP cursor called 'results'.
            lcCommand = "select top 1000 logComment from [umbraco].[dbo].[umbracoLog]"
            lnResult = SQLExec(lnHandle, lcCommand, "results")

            If lnResult = -1
                Messagebox("Error running SELECT.", 48, "Error!")
            Else

                * -- Got some rows, populate combobox.
                Select results
                Scan

                    * -- Transform() just stringifies the column.
                    Thisform.cboSql.AddItem(Transform(results.logComment))

                Endscan

            Endif

            * -- Tidy up.
            Use In Select("results")
            SQLDisconnect(lnHandle)

        Else

            Messagebox("Couldn't connect to server.", 48, "Error!")
            llReturn =.F.

        Endif

    Endif

Return llReturn

Well, here's how I'd do it programmatically assuming a combobox called 'cboSQL' on the form, and this code in the form Init() method (this only does one column in the combo but check the VFP help under AddItem() ):

Local lnHandle, lnResult As Integer
Local lcConnstring, lcCommand As String
Local llReturn As Boolean

    * -- Do the default behaviour.
    llReturn = DoDefault()

    If llReturn

        * -- Try to connect to a local SQL Express 2008 instance.
        lcServername = ".\SQLEXPRESS"
        lcDbname = "umbraco"
        lcConnstring = [server=]+ lcServername+ [;driver={SQL Server};database=]+ lcDbname
        lcConnstring = lcConnstring + [;DSN='';Trusted_Connection=Yes]

        * -- Try to connect and get a connection handle.
        lnHandle = Sqlstringconnect(lcConnstring)

        * -- Got a connection ?
        If lnHandle > 0

            * -- Run a query, put the results in a VFP cursor called 'results'.
            lcCommand = "select top 1000 logComment from [umbraco].[dbo].[umbracoLog]"
            lnResult = SQLExec(lnHandle, lcCommand, "results")

            If lnResult = -1
                Messagebox("Error running SELECT.", 48, "Error!")
            Else

                * -- Got some rows, populate combobox.
                Select results
                Scan

                    * -- Transform() just stringifies the column.
                    Thisform.cboSql.AddItem(Transform(results.logComment))

                Endscan

            Endif

            * -- Tidy up.
            Use In Select("results")
            SQLDisconnect(lnHandle)

        Else

            Messagebox("Couldn't connect to server.", 48, "Error!")
            llReturn =.F.

        Endif

    Endif

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