如何通过SqlDataReader获取数据.按列名GetValue

本文关键字:GetValue 数据 何通过 SqlDataReader 获取 | 更新日期: 2023-09-27 18:19:07

我使用SqlDataReader。GetValue方法从DB中读取值:

Log.WriteLine("Value of CompanyName column:" + thisReader.GetValue(1)); 

作为参数GetValue获取列的索引。我如何指定列名而不是索引?

如何通过SqlDataReader获取数据.按列名GetValue

Log.WriteLine("Value of CompanyName column:" + thisReader["CompanyName"]); 

您也可以这样做。

//find the index of the CompanyName column
int columnIndex = thisReader.GetOrdinal("CompanyName"); 
//Get the value of the column. Will throw if the value is null.
string companyName = thisReader.GetString(columnIndex);

thisReader。GetString (int columnIndex)