我使用SqlDataReader.GetValue方法从数据库中读取值:
Log.WriteLine("Value of CompanyName column:" + thisReader.GetValue(1));
作为参数,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)
这个读取器.GetString(列索引)
Log.WriteLine("CompanyName列的值:" + thisReader.GetValue(thisReader.GetOrdinal("CompanyName")));
方括号 [] 代表什么? - algotreturn GetValue(GetOrdinal(name));
参考 https://referencesource.microsoft.com/#system.data/system/data/sqlclient/SqlDataReader.cs - Mark SchultheissGetInt32
这样的转换方法时,这种方法非常有用。 - Brian Lacy