string query =
"SELECT * FROM table1, table2 WHERE table1.Id = table2.fId";
...
using(IDataReader dataReader =
db.ExecuteReader(CommandType.Text, query))
..
string value = dataReader["table2.field"]; //dies
Run Code Online (Sandbox Code Playgroud)
我目前正在编写一些.NET代码,它涉及执行连接查询,然后使用DataReader访问返回的数据.我想知道是否可以使用某种前缀表示法访问返回行中的字段(参见上面的示例),而不必使用序号位置来访问行中的值(在两个表都包含重叠字段的情况下)名)?
鉴于您提前知道字段名称,为什么不在select语句中表达它们?
string query = "SELECT table2.field as field2, ... FROM table1, table2 " +
"WHERE table1.Id = table2.fId";
Run Code Online (Sandbox Code Playgroud)
然后你可以参考dataReader["field2"].