任何人都可以帮我这个,我想从查询列ZPZ_Von获取数据输入我放入循环的新时间而在datagridview中打印,我当前收到错误(System.InvalidOperationException:“此命令已经有一个与之关联的打开DataReader,必须先关闭它。“).. 我该怎么办?
这是我在按钮中的代码。
using (SqlConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["TestControl.Properties.Settings.DB"].ConnectionString))
{
boAPI4.Login login = new boAPI4.Login();
string cS = login.GetConnectionString();
DataAccess dA = new DataAccess(cS);
int userID = dA.getLpeID(login.GetBoUserNr());
PRAESENZZEIT q = new PRAESENZZEIT();
q.ZPZ_LPE_ID = userID;
if (db.State == ConnectionState.Closed)
db.Open();
string query = "SELECT zei.ZPZ_Von, zei.ZPZ_Bis, per.LPE_Nr, zei.ZPZ_LPE_ID, zei.ZPZ_Datum, SUM (zei.ZPZ_Std100) AS ZPZ_Std100" +
" FROM DB.dbo.Z_PRAESENZZEIT zei INNER JOIN DB.dbo.A_PERSONAL per ON zei.ZPZ_LPE_ID = per.LPE_ID" +
$" WHERE zei.ZPZ_Datum BETWEEN '{dtFromDate.Value}' AND '{dtToDate.Value}' AND zei.ZPZ_LPE_ID='{userID.ToString()}' GROUP BY per.LPE_Nr, zei.ZPZ_LPE_ID, zei.ZPZ_Datum, zei.ZPZ_Von, zei.ZPZ_Bis ORDER BY zei.ZPZ_Datum, per.LPE_Nr;";
using (SqlCommand cmd = new SqlCommand(query, db))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
var items = new List<PRAESENZZEIT>();
while (dr.Read())
{
PRAESENZZEIT pra = new PRAESENZZEIT();
pra.ZPZ_Von = Convert.ToDateTime(dr["ZPZ_Von"]);
if (pra.ZPZ_Von.TimeOfDay < new TimeSpan(8, 5, 0))
pra.ZPZ_Von = new DateTime(pra.ZPZ_Von.Year, pra.ZPZ_Von.Month, pra.ZPZ_Von.Day, 8, 0, 0);
// DateTime gehen = DateTime.Now;
pra.ZPZ_Bis = Convert.ToDateTime(dr["ZPZ_Bis"]);
pra.arbeitszeit = pra.ZPZ_Bis - pra.ZPZ_Von;
}
pRAESENZZEITBindingSource.DataSource = items;
}
}
}
这是我的班级PRAESENZZEIT
public class PRAESENZZEIT
{
public int LPE_Nr { get; set; }
public DateTime ZPZ_Datum { get; set; }
public double ZPZ_Std100 { get; set; }
public int ZPZ_LPE_ID { get; set; }
public DateTime ZPZ_Von { get; set; }
public DateTime ZPZ_Bis { get; set; }
public DateTime ZPZ_Std { get; set; }
public int ZPZ_ID { get; set; }
public int ZPZ_Jahr { get; set; }
public int ZPZ_Monat { get; set; }
public int ZPZ_Tag { get; set; }
public DateTime ZPZ_ERFDAT { get; set; }
public string ZPZ_ERFUSER { get; set; }
public DateTime ZPZ_MUTDAT { get; set; }
public string ZPZ_MUTUSER { get; set; }
public TimeSpan arbeitszeit { get; set; }
}
这是数据库数据
谢谢大家的帮助
答案 0 :(得分:0)
由于您不希望将更改保留到数据库,因此可以将要创建的对象存储在List
中,并将其用作数据源:
using(SqlCommand cmd = new SqlCommand(query, db))
{
using(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using(SqlDataReader dr = cmd.ExecuteReader())
{
var items = new BindingList<PRAESENZZEIT>();
while (dr.Read())
{
PRAESENZZEIT pra = new PRAESENZZEIT();
pra.ZPZ_Von = Convert.ToDateTime(dr["ZPZ_Von"]);
if (pra.ZPZ_Von.TimeOfDay < new TimeSpan(8, 5, 0))
pra.ZPZ_Von = new DateTime(pra.ZPZ_Von.Year, pra.ZPZ_Von.Month, pra.ZPZ_Von.Day, 8, 0, 0);
// DateTime gehen = DateTime.Now;
pra.ZPZ_Bis = Convert.ToDateTime(dr["ZPZ_Bis"]);
pra.arbeitszeit = pra.ZPZ_Bis - pra.ZPZ_Von;
}
pRAESENZZEITBindingSource.DataSource = items;
}
}
}
您只需在班级中添加新属性arbeitszeit
即可存储pra.ZPZ_Bis - pra.ZPZ_Von
结果(如果您愿意)。
注意我为所有实现的对象添加了
using
语句IDisposable
,就像您已经为SqlConnection
所做的那样。