在asp與asp.net存取vfp的檔案

在 asp 或 asp.net 的程式中要存取Visual Foxpro (vfp) 的資料檔(table),作法如下。

首先需下載 微軟公司提供的 VFP 的 oledb,並且在web server上安裝。

與存取資料庫的差別,只在於建立connection時的connection string不同而已,建立connection之後,即與存取一般資料庫是一樣的。所以,在此只提供建立connection的程式碼。

在 asp 的範例如下

Function getConn()

Dim DB, dpath

dpath = Server.Mappath(".")

On Error Resume Next

SET DB=server.createobject("ADODB.Connection")

DB.Open "Provider=vfpoledb;Data Source=" & dpath & "\allsco.dbf;" & _

"Mode=Share Deny None;Password='';Collating Sequence=MACHINE"

If Err.Number <> 0 Then

Response.Write("<h2>System Error:</h2>")

' Don't use Err.Description.

' it will show error detail, maybe put the system to danger.

Response.Write(Err.Number & "<br />" & Err.Description & "<br />")

Response.Write("Cannot connect to DB server!<br />")

Response.Write("Please contact the system manager.")

Response.End

End If

On Error Goto 0

set getConn=DB

End Function

在 asp.net 的範例如下

<%@ Import Namespace="System.Data.OleDb" %>

<script runat="server">

Dim dpath = Server.Mappath(".")

Dim sConnectionString = "Provider=vfpoledb;Data Source=" & dpath & ";" & _

"Mode=Share Deny None;Password='';Collating Sequence=MACHINE"

Function getConn()

Dim xconn = new OleDbConnection()

Try

xconn.ConnectionString = sConnectionString

xconn.Open()

Catch e as Exception

Response.Write("Connet to SQL error:" & e.message)

End Try

Return xconn

End Function

</script>