質問

In VBA I might have something like this:

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

Im trying to do the same thing in C#, However

    result = recordSet("Column Name");

wouldnt work because C# uses square brackets for collections. But this doesnt seem to work either:

    result = recordSet["Column Name"];

Any ideas on the C# equivalent of the above VBA code?

EDIT: Here is the full VBA code Im trying to convert to put it into context

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
役に立ちましたか?

解決

result = recordSet.Fields["Column Name"].Value;

他のヒント

Is a DataSet or DataTable maybe what you are looking for?

Edit: Try something like this (not tested yet and some error handling is needed):

    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();
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top