我知道我们可以使用using
在sqlconnection
中拥有多个命令。
像这样:
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand command1 = new SqlCommand(commandText1, connection))
{
}
using (SqlCommand command2 = new SqlCommand(commandText2, connection))
{
}
// etc
}
但是,如果using
位于返回阅读器强制转换的方法中,该怎么办?
像这样:
public IEnumerable<LocationInfo> GetData()
{
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString))
{
//connection.Close();
connection.Open();
using (SqlCommand command = new SqlCommand(@"SELECT .... ", connection))
{
command.Notification = null;
SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
if (connection.State == ConnectionState.Closed)
{ connection.Open(); }
using (var reader = command.ExecuteReader())
{
return reader.Cast<IDataRecord>().Select(x => new LocationInfo()
{
Names = x.GetString(2),
Values = Math.Round(x.GetDouble(7), 2).ToString("#,##0.00"),
ValuesDouble = x.GetDouble(7),
Values2 = Math.Round(x.GetDecimal(9), 2).ToString("#,##0.00"),
ValuesDouble2 = x.GetDecimal(9),
truckDelivery=x.GetDecimal(3),
truckIdle = x.GetDecimal(4),
truckRepair = x.GetDecimal(5),
truckReady = x.GetDecimal(6),
presentEmp=x.GetInt32(11),
absentEmp = x.GetInt32(12),
ondutyEmp = x.GetInt32(13),
}).ToList();
}
/* I tried this but it just got ignored
using (var reader2 = command.ExecuteReader())
{
reader2.NextResult();
return reader2.Cast<IDataRecord>().Select(x => new LocationInfo()
{
SumVol = x.GetString(0)
}).ToList();
}*/
}
}
}
请帮帮我。我的第二个using
一直被忽视,并且不会认为我知道任何事情,因为我对此不熟悉。提前谢谢。
答案 0 :(得分:0)
您需要读取ExecuteReader获取的SqlDataReader指向的记录。在List中累积LocationInfo,并在完成读取器循环后返回它们。
Thats my class.
class MyClass1 : IInterface1
{
public MyClass1()
{
this.Interface2s = new List<IInterface2>();
}
public string strI1 { get; set; }
public int intI1 { get; set; }
public IList<IInterface2> Interface2s { get; set; }
public int intC1 { get; set; }
}
答案 1 :(得分:0)
好的,所以你可以在这种情况下将两个结果集的结果合并到一个对象中。
public IEnumerable<LocationInfo> GetData()
{
List<LocationInfo> locations = new List<LocationInfo>();
using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString))
using (SqlCommand command = new SqlCommand(@"SELECT .... ", connection))
{
connection.Open();
using (var reader = command.ExecuteReader())
{
LocationInfo x = new LocationInfo();
while(reader.Read())
{
{
x.Names = reader.GetString(2),
x.Values=Math.Round(reader.GetDouble(7),2).ToString("#,##0.00"),
x.ValuesDouble = reader.GetDouble(7),
Values2 = Math.Round(reader.GetDecimal(9), 2).ToString("#,##0.00"),
x.ValuesDouble2 = reader.GetDecimal(9),
x.truckDelivery=reader.GetDecimal(3),
x.truckIdle = reader.GetDecimal(4),
x.truckRepair = reader.GetDecimal(5),
x.truckReady = reader.GetDecimal(6),
x.presentEmp=reader.GetInt32(11),
x.absentEmp = reader.GetInt32(12),
x.ondutyEmp = reader.GetInt32(13),
};
}
if(reader.NextResult())
{
while (reader.Read())
{
x.SumVol=reader.GetString(0);
}
}
locations.Add(x);
}
}
return locations;
}