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

时间:2017-12-19 09:25:32

标签: c# sql .net generic-list

任何人都可以指出我犯了错误的地方,我想把一个新的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。

enter image description here

因此,我需要通过日期时间变量

的数据

这是它如何通过变量但返回结果,好像它没有解决基础数据,问题是什么?我明白,如果时间[ZPZ_VON 07:45]退出应该是08:00 ..

SQL QUERY SELECT ZPZ_Von,ZPZ_bis FROM DB.dbo.Z_PRAESENZZEIT ZPZ_LPE_ID =&#39; 196&#39;

代码:

 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();

数据库查询结果:

look at the photo

1 个答案:

答案 0 :(得分:1)

您正在pra循环之外初始化while类。每次从listid阅读DataRow时,哪些内容都不会在列表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 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;  
}