问题
I have occurred problem in getting my row value, once I read it from Database using OleDbCommand
Current code:
private static string TryCheckDateTime(OleDbConnection cn, string table, string colName, string dataColumn)
{
if (dataColumn.Contains(AllowedValues.String.ToString()))
{
using (OleDbCommand cmd = new OleDbCommand())
{
OleDbDataReader rowItems;
// Connection to Database
cmd.Connection = cn;
// Query script: "SELECT TOP 5 {0} FROM [DBName].[dbo].[{1}]"
cmd.CommandText = string.Format(Values.VerifyDatetime, colName, table);
// Gets top 5 rows
rowItems = cmd.ExecuteReader(CommandBehavior.Default);
foreach (var row in rowItems)
{
// how can I get that row data?
var a = row;
}
}
}
return null;
}
And value, that I want to get:
回答1:
Do you tried
string test;
while (rowItems.Read())
test = rowItems[0].ToString();
?
回答2:
Maybe this could help. Use .HasRows first then .Read()
if (rowItems.HasRows)
{
rowItems.Read();
foreach(var row in rowItems)
{
var a = row["sampleonly"].ToString(); // or something
}
}
else
{
// do something
}
来源:https://stackoverflow.com/questions/33261289/get-database-row-column-value-with-oledbcommand