使用Oledb提供程序的Visual Foxpro数据库存储过程

时间:2013-01-05 11:39:11

标签: stored-procedures visual-foxpro

我正在使用Visual Foxpro 8.0数据库。下面的程序我用来根据条件匹配从数据库返回记录,但它引发了错误:

  

“功能未实现。”

Foxpro程序------------------------

PROCEDURE FX_Proc_ValidateUser (paramUserName AS Character, paramPassword AS Character)
LOCAL VarUserName AS Character, varXml
VarUserName = IIF(VARTYPE(paramUserName)!="N","",paramUserName)
LOCAL VarPassword AS Character
VarPassword = IIF(VARTYPE(paramPassword)!="N","",paramPassword)
SELECT userinfoid, ;
        username, ;
        password ;
FROM tm_userinfo.dbf ;
WHERE username = VarUserName AND password = VarPassword ;
INTO CURSOR procResult  
varXml = ""
CURSORTOXML("procResult","varXml",1,32,0,"1")
RETURN varXml
ENDPROC

用于调用此过程的前端代码------------------

string ConnectionString = "Provider=VFPOLEDB.1;Data Source=C:\Users\raj\Documents\Visual FoxPro Projects\dbFoxMaster.dbc;Collating Sequence=machine;" providerName="System.Data.OleDb.OleDbConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
OledbConnection objOleDbConnection = new OleDbConnection(ConnectionString);
objOleDbConnection.Open();
OleDbCommand objOleDbCommand = new OleDbCommand();
objOleDbCommand.CommandType = CommandType.StoredProcedure;
objOleDbCommand.CommandText = "FX_Proc_ValidateUser";
objOleDbCommand.Connection = objOleDbConnection;
objOleDbCommand.Parameters.Add("paramUserName", OleDbType.Char).Value = "abc";
objOleDbCommand.Parameters.Add("paramPassword", OleDbType.Char).Value = "123";
var xmlString = oOleDbCommand.ExecuteScalar().ToString();
    DataTable table = new DataTable();
    using (var reader = new StringReader(xmlString))
    {
        var dataSet = new DataSet();

        // creating a dataset from the xml 
        dataSet.ReadXml(reader);
        table = dataSet.Tables[0];
    }

如何使用OledbCommand从foxpro 8.0存储过程获取结果集?

3 个答案:

答案 0 :(得分:3)

看起来问题是您将结果放入数组中,并且只返回数组中的第一项。您应该更改存储过程,以便它使用游标。

VFP9示例

以下是northwind.dbc中的存储过程示例: enter image description here

以下是使用C#调用存储过程的示例:

var northwindDbcPath = @"C:\Program Files (x86)\Microsoft Visual FoxPro 9\Samples\Northwind\Northwind.dbc";
var connectionString = "Provider=VFPOLEDB.1;Data Source=" + northwindDbcPath;
var table = new DataTable();

using(var connection = new OleDbConnection(connectionString)) {
    using(var command = connection.CreateCommand()) {
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "CustOrdersDetail";
        command.Parameters.Add("x", 10248);

        var adapter = new OleDbDataAdapter(command);

        adapter.Fill(table);
    }
}

VFP8示例: (我没有VFP8的副本,但我相信这会有效) enter image description here

var northwindDbcPath = @"C:\Program Files (x86)\Microsoft Visual FoxPro 9\Samples\Northwind\Northwind.dbc";
var connectionString = "Provider=VFPOLEDB.1;Data Source=" + northwindDbcPath;
DataTable table;

using(var connection = new OleDbConnection(connectionString)) {
    using(var command = connection.CreateCommand()) {
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "CustOrdersDetail2";
        command.Parameters.AddWithValue("x", 10248);

        connection.Open();      
        // executing stored procedure and getting xml result
        var xml = command.ExecuteScalar() as string;

        connection.Close();

        using(var reader = new StringReader(xml)) {
            var dataSet = new DataSet();

            // creating a dataset from the xml 
            dataSet.ReadXml(reader);
            table = dataSet.Tables[0];
        }
    }
}

答案 1 :(得分:0)

我没有VFP,上次我使用VFP时仍然是版本3.0 ...

我会根据规范here尝试这样的事情。要了解其余的阅读here

PROCEDURE insertData (pusername, ppassword)

                    SELECT userinfoid, username, password 
                    FROM tm_userinfo.dbf 
                    WHERE username = pusername AND password = ppassword
                    INTO ARRAY results
     RETURN results
ENDPROC

答案 2 :(得分:0)

Visual Foxpro存储过程基本上使用Visual Foxpro代码来执行诸如在插入或更新或删除时验证列的任务。

例如,您可以将以下代码添加到VFP数据库存储过程以验证“状态”列:

PROCEDURE ValidateState()
IF address.state <> [OH]
    MESSAGEBOX([Incorrect State value!], 48, [Invalid State])
ENDIF
ENDPROC

要查询Foxpro数据库中的表,您可能需要查看创建本地视图。您可以通过右键单击foxpro数据库并选择“新建本地视图”来执行此操作,然后使用查询构建器定义视图。在“过滤器”选项卡的“示例”列下,可以使用“?”定义参数。例如“?pusername”。

以下是从Foxpro代码调用视图的方法:

 LOCAL pusername AS Integer   
 pusername = "SomeUserName"    &&Use this to filter the view
 SELECT 0
 USE MyViewName       &&This will call the view and perform the filter.

您还可以使这些视图可更新并对视图执行插入。