读取数据库数据并转发给变量

Reading database data and forwarding to variables

任何人都可以指出我在哪里犯了错误,我想放置一个新的 sql 查询,我将从数据库中读取数据并将它们放入我的函数中,最后它们会显示在当前出现在第一个查询 SUM (zei.ZPZ_Std100) AS ZPZ_Std100

中的冒号 ("stunden") 的出口处

这看起来像是我在按钮中的全部代码

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 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 ORDER BY zei.ZPZ_Datum, per.LPE_Nr;";

            pRAESENZZEITBindingSource.DataSource = db.Query<PRAESENZZEIT>(query, commandType: CommandType.Text);

            List<PRAESENZZEIT> listid = new List<PRAESENZZEIT>();
            PRAESENZZEIT pra = new PRAESENZZEIT();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestControl.Properties.Settings.DB"].ConnectionString);
            string sql = "SELECT ZPZ_Von,ZPZ_bis FROM  DB.dbo.Z_PRAESENZZEIT WHERE ZPZ_LPE_ID='196'";
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                pra.ZPZ_Von = Convert.ToDateTime(dr["ZPZ_Von"]);
                pra.ZPZ_Bis = Convert.ToDateTime(dr["ZPZ_bis"]);
                listid.Add(pra);
            }
            dataGridView1.DataSource = listid;
            con.Close();

            DateTime kommen = DateTime.Now;

            kommen = pra.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;
            gehen = pra.ZPZ_Bis;
            TimeSpan arbeitszeit = pra.ZPZ_Bis - pra.ZPZ_Von;

        }

目前,在出口我得到 0。

所以,我需要通过日期时间变量的数据

这是它处理变量的方式,但是 returns 结果好像没有处理基础数据,有什么问题?我理解如果时间 [ZPZ_VON 07:45] 出口应该是 08:00..

SQL 查询 SELECT ZPZ_Von,ZPZ_bis 从 DB.dbo.Z_PRAESENZZEIT 哪里 ZPZ_LPE_ID='196'

代码:

 List<PRAESENZZEIT> listid = new List<PRAESENZZEIT>();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestControl.Properties.Settings.DB"].ConnectionString);
            string sql = "SELECT ZPZ_Von, ZPZ_bis FROM  DB.dbo.Z_PRAESENZZEIT WHERE ZPZ_LPE_ID='196'";
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlDataReader dr = cmd.ExecuteReader();

            DataTable dt = new DataTable();


            while (dr.Read())
            {
                PRAESENZZEIT pra = new PRAESENZZEIT();
                pra.ZPZ_Von = Convert.ToDateTime(dr["ZPZ_Von"]);
                pra.ZPZ_Bis = Convert.ToDateTime(dr["ZPZ_bis"]);
                listid.Add(pra);

                DateTime kommen = DateTime.Now;

                kommen = pra.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;
                gehen = pra.ZPZ_Bis;
                TimeSpan arbeitszeit = pra.ZPZ_Bis - pra.ZPZ_Von;
            }
            con.Close();

数据库查询结果:

您正在 while 循环之外初始化 pra class。每次从 DataReader.

读取 DataRow 时不会在列表 listid 中添加新记录

试试下面的代码:

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 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 ORDER BY zei.ZPZ_Datum, per.LPE_Nr;";

    pRAESENZZEITBindingSource.DataSource = db.Query<PRAESENZZEIT>(query, commandType: CommandType.Text);

    List<PRAESENZZEIT> listid = new List<PRAESENZZEIT>();
    //PRAESENZZEIT pra = new PRAESENZZEIT(); //Needs to be inside the while loop.
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TestControl.Properties.Settings.DB"].ConnectionString);
    string sql = "SELECT ZPZ_Von,ZPZ_bis FROM  DB.dbo.Z_PRAESENZZEIT WHERE ZPZ_LPE_ID='196'";
    con.Open();
    SqlCommand cmd = new SqlCommand(sql, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);

    var listid = ConvertDataTable<PRAESENZZEIT>(dt);

    dataGridView1.DataSource = listid;
    con.Close();
}

private static List<T> ConvertDataTable<T>(DataTable dt)  
{  
   List<T> data = newList<T>();  
   foreach (DataRowrow in dt.Rows)  
   {  
      Titem = GetItem<T>(row);  
      data.Add(item);  
   }  
   return data;  
}  

private static TGetItem<T>(DataRow dr)  
{  
   Type temp = typeof(T);  
   T obj =Activator.CreateInstance<T>();  
   foreach (DataColumncolumn in dr.Table.Columns)  
   {  
      foreach (PropertyInfopro in temp.GetProperties())  
      {  
         if (pro.Name == column.ColumnName)  
         pro.SetValue(obj,dr[column.ColumnName], null);  
         else  
         continue;  
      }  
   }  
   return obj;  
}