0

在 VBA 中,我可能有这样的东西:

    Dim recordSet As DAO.recordSet  
    result = recordSet("Column Name")

我试图在 C# 中做同样的事情,但是

    result = recordSet("Column Name");

不会工作,因为 C# 使用方括号进行集合。但这似乎也不起作用:

    result = recordSet["Column Name"];

关于上述 VBA 代码的 C# 等价物有什么想法吗?

编辑:这是我试图将其转换为上下文的完整 VBA 代码

Public Function GetColumnValues( _
                                 database As database, _
                                 column As String, _
                                 table As String _
                               ) As String()

Dim sqlQuery As String
Dim recordSet As DAO.recordSet
Dim recordCount As Integer
Dim results() As String

sqlQuery = "SELECT [" + table + "].[" + column + "]" & _
           "FROM [" + table + "];"


Set recordSet = database.OpenRecordset(sqlQuery)
recordSet.MoveLast             
recordSet.MoveFirst
recordCount = recordSet.recordCount

ReDim results(recordCount) As String

For i = 1 To recordCount
    results(i) = recordSet(column)
    recordSet.MoveNext
Next i

recordSet.Close

GetColumnValues = results

End Function
4

2 回答 2

3
result = recordSet.Fields["Column Name"].Value;
于 2013-08-31T23:40:52.847 回答
1

DataSet 或 DataTable 可能是您正在寻找的吗?

编辑:尝试这样的事情(尚未测试,需要一些错误处理):

    public string[] GetColumnValues(string connectionString, string table, string column)
    {
        var connection = new SqlConnection(connectionString);
        var dataAdapter = new SqlDataAdapter(
            string.Format("SELECT [{0}].[{1}] FROM [{0}]", table, column), connection);
        var result = new List<string>();

        connection.Open();

        var dataSet = new DataSet();
        dataAdapter.Fill(dataSet);

        if (dataSet.Tables.Count > 0)
        {
            result.AddRange(from DataRow row in dataSet.Tables[0].Rows select row[0].ToString());
        }

        connection.Close();

        return result.ToArray();
    }
于 2013-08-31T22:34:07.197 回答