[英]DAO difference between C# and VBA RecordSets
In VBA I might have something like this: 在VBA中,我可能会遇到以下情况:
Dim recordSet As DAO.recordSet
result = recordSet("Column Name")
Im trying to do the same thing in C#, However 我试图在C#中做同样的事情,但是
result = recordSet("Column Name");
wouldnt work because C# uses square brackets for collections. 不会工作,因为C#将方括号用于集合。 But this doesnt seem to work either: 但这似乎也不起作用:
result = recordSet["Column Name"];
Any ideas on the C# equivalent of the above VBA code? 关于上述VBA代码的C#等效概念?
EDIT: Here is the full VBA code Im trying to convert to put it into context 编辑:这是完整的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
result = recordSet.Fields["Column Name"].Value;
Is a DataSet or DataTable maybe what you are looking for? 您正在寻找DataSet或DataTable吗?
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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.