我开发web应用程序asp.net连接到visual fox pro数据库免费表(.dbf)与microsoft oledb for visualfoxpro驱动程序。 当我用vs2013运行程序时,开发连接工作正常。 但是当我的网站在网络服务器上运行时,它无法搜索或连接到vfp数据库 我收到此错误:无效的路径或文件名
这是我连接vfp数据库的代码。
Try
'if use visual fox pro Connect to a single DBF-file
ConnectionString = "Provider=vfpoledb;Data Source=//database/event/event/OPD/OUT.DBF;sourcetype=DBF;exclusive=No;Collating Sequence=machine; User ID=stat1;Password = stat1;"
dBaseConnection = New OleDb.OleDbConnection(ConnectionString)
dBaseConnection.Open()
sCommand = "SELECT lastdate,save_id,count(*) AS Tcoder FROM OUT.DBF WHERE lastdate BETWEEN CTOD('" & TextBox1.Text & "') and CTOD('" & TextBox2.Text & "') AND typeevent LIKE '%" & TextBox3.Text & "%' "
sCommand &= "AND lasttime >= '" & ddlFromTime.SelectedValue & "' AND lasttime <= '" & ddlToTime.SelectedValue & "' GROUP BY lastdate,save_id ORDER BY Tcoder DESC "
dBaseCommand = New OleDb.OleDbCommand(sCommand, dBaseConnection)
'
Dim dt As DataTable = GetData(dBaseCommand)
GridView1.DataSource = dt
GridView1.DataBind()
'
dBaseConnection.Close()
Catch ex As OleDb.OleDbException
Response.Write("Error: " + ex.Message)
End Try
如何解决这个问题?预先感谢。 - / -
答案 0 :(得分:0)
首先,您的连接字符串应仅指向表所在的PATH,而不是.dbf文件的实际名称。然后,您可以从位于连接路径中的任何.dbf进行查询。
其次,在构建查询时,通过执行字符串连接来避免SQL-Injection。为了防止,VFP使用“?”作为参数的占位符,需要以与查询中显示的顺序相同的顺序添加参数。
ConnectionString = "Provider=vfpoledb;Data Source=//database/event/event/OPD;sourcetype=DBF;exclusive=No;Collating Sequence=machine; User ID=stat1;Password = stat1;"
dBaseConnection = New OleDb.OleDbConnection(ConnectionString)
dBaseConnection.Open()
' Notice all the "?" place-holders for the parameters.
sCommand =
@"SELECT lastdate, save_id, count(*) AS Tcoder
FROM OUT.DBF
WHERE BETWEEN( lastdate, ?, ? )
AND typeevent LIKE ?
AND lasttime >= ?
AND lasttime <= ?
GROUP BY
lastdate,
save_id
ORDER BY
Tcoder DESC ";
' get the command instance
dBaseCommand = New OleDb.OleDbCommand(sCommand, dBaseConnection)
' NOW, add the parameters IN ORDER as they appear... starting with the dates
' but you will have to convert the ".Text" values to a date field in VB.net
' first, then the date-based field will be properly recognized in the VFP call. Sorry not fluent in VB to convert, but you should be able to get that.
dBaseCommand.Parameters.AddWithValue( "parmFromDate", TextBox1.Text );
dBaseCommand.Parameters.AddWithValue( "parmFromDate", TextBox2.Text );
' now for the "TypeEvent" criteria, no wrapping the text within quotes
' as the query knows it is a string, so don't explicitly add quotes.
dBaseCommand.Parameters.AddWithValue( "parmTypeEvent", "%"+ TextBox3.Text +"%");
' It appears your LASTTIME field are string-based, so no conversion
' just strings like other.
dBaseCommand.Parameters.AddWithValue( "parmTime1", ddlFromTime.SelectedValue );
dBaseCommand.Parameters.AddWithValue( "parmTime2", ddlToTime.SelectedValue );
' Now, finish getting the data and setting the Data source to the result
Dim dt As DataTable = GetData(dBaseCommand)
GridView1.DataSource = dt
GridView1.DataBind()
dBaseConnection.Close()