在VBA中我可能有这样的事情:
Dim recordSet As DAO.recordSet
result = recordSet("Column Name")
我试图用C#做同样的事情但是
result = recordSet("Column Name");
无效,因为C#对集合使用方括号。 但这似乎也不起作用:
result = recordSet["Column Name"];
关于C#等同于上述VBA代码的任何想法?
编辑:以下是我试图将其转换为上下文的完整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
答案 0 :(得分:2)
result = recordSet.Fields["Column Name"].Value;
答案 1 :(得分: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();
}