使用 OleDbCommand 获取数据库行列值
get Database row column value with OleDbCommand
我在使用 OleDbCommand
从数据库读取行值时遇到问题
当前代码:
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;
}
以及我想要获得的价值:
你试过了吗
string test;
while (rowItems.Read())
test = rowItems[0].ToString();
?
也许这会有所帮助。先使用 .HasRows 然后使用 .Read()
if (rowItems.HasRows)
{
rowItems.Read();
foreach(var row in rowItems)
{
var a = row["sampleonly"].ToString(); // or something
}
}
else
{
// do something
}
我在使用 OleDbCommand
当前代码:
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;
}
以及我想要获得的价值:
你试过了吗
string test;
while (rowItems.Read())
test = rowItems[0].ToString();
?
也许这会有所帮助。先使用 .HasRows 然后使用 .Read()
if (rowItems.HasRows)
{
rowItems.Read();
foreach(var row in rowItems)
{
var a = row["sampleonly"].ToString(); // or something
}
}
else
{
// do something
}