get Database row column value with OleDbCommand

回眸只為那壹抹淺笑 提交于 2019-12-11 10:33:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!