Combobox SQL Server 2005 Express表中的VFP组合框内容
我还是VFP的新手,我正在寻找关于如何将SQL Server表字段拉入VFP ComboBox或其他对象(如果更好的话)的建议和建议,就像自动完成一样,但来自SQL Server数据库 我在表中有大约2列和1000行,组合框应该只显示第二列字段供用户选择,但使用选择的第一列字段记录到另一个表中。我希望你能明白Combobox SQL Server 2005 Express表中的VFP组合框内容,combobox,sql-server-express,visual-foxpro,Combobox,Sql Server Express,Visual Foxpro,我还是VFP的新手,我正在寻找关于如何将SQL Server表字段拉入VFP ComboBox或其他对象(如果更好的话)的建议和建议,就像自动完成一样,但来自SQL Server数据库 我在表中有大约2列和1000行,组合框应该只显示第二列字段供用户选择,但使用选择的第一列字段记录到另一个表中。我希望你能明白 提前感谢您的反馈。好吧,下面是我如何通过编程实现的,假设表单上有一个名为“cboSQL”的组合框,并且表单Init方法中的这段代码在组合框中只执行一列,但请检查AddItem下的VFP帮助
提前感谢您的反馈。好吧,下面是我如何通过编程实现的,假设表单上有一个名为“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
我的处理方式会有点不同,因为您需要两列。。一个显示,一个用于实际数据。组合框可以直接绑定到表或光标,光标只不过是一个临时表,在关闭时会自动擦除 在组合框的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