
private static string connectionString = "server=10.31.180.3;Port=5432;Database=postgres;uid=postgres;pwd=hik12345+";
//创建数据库连接对象
NpgsqlConnection con = new NpgsqlConnection(connectionString);
private void GetHistory()
{
int addCount = 0,updateCount=0;
DateTime maxDate = new BLL.Passenger_Day_srcBLL().GetMax("create_time");//最大时间
if (maxDate.Subtract(DateTime.Now).Days == 0)
{ //最大时间如果是今天 ,查找
maxDate = DateTime.Now.Date;
}
//定义查询语句,这里最好将SQL语句在SQL中写好并验证正确确在复制粘贴过来(在对数据查询时最好只查所需的一些不需要的数据就不要取出,这样可以提高运行的效率)
string strSql = " select sum(case when date_no = cast(to_char(now(),'yyyymmdd') as integer) then 1 else 0 end from dwb_gov_office_item_list_min";
NpgsqlDataAdapter sda = new NpgsqlDataAdapter(strSql, con);
DataSet ds = new DataSet();
sda.Fill(ds);//把执行得到的数据放在数据集中 ---执行到这里就报错 提示 42P01 关系“[dwb_gov_office_item_list_min]”不存在
// dataGridViewTest.DataSource = ds.Tables[0];
con.Close();
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
int devId = 0;
Model.D_Device devModel = new BLL.D_DeviceBLL().GetModel(" and Remarks='" + dr["src_index"].ToString() + "'");
if (devModel != null)
{
devId = devModel.DevId;
}
// string strWhere = " and DataId='" + dr["id"].ToString() + "' and create_time='" + ((DateTime)dr["create_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff") + "' ";
string strWhere = " and DataId='" + dr["id"].ToString() + "'";
Model.Passenger_Day_src modelPDay = new BLL.Passenger_Day_srcBLL().GetModel(strWhere);
if (modelPDay != null)
{
modelPDay.update_time = ((DateTime)dr["update_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff");//修改时间
modelPDay.enter_count = int.Parse(dr["enter_count"].ToString());//进人数
modelPDay.exit_count = int.Parse(dr["exit_count"].ToString());//出人数
new BLL.Passenger_Day_srcBLL().Update(modelPDay);
updateCount ++;
}
else
{
modelPDay = new Model.Passenger_Day_src();
modelPDay.DevId = devId;
modelPDay.DataId = dr["id"].ToString();
modelPDay.create_time = ((DateTime)dr["create_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff");//创建时间
modelPDay.update_time = ((DateTime)dr["update_time"]).ToString("yyyy-MM-dd HH:mm:ss.fff");//修改时间
modelPDay.date_time = dr["date_time"].ToString();//统计时间
modelPDay.src_index = dr["src_index"].ToString();//监控点ID
modelPDay.src_dev_index = dr["src_dev_index"].ToString();//资源点ID
modelPDay.enter_count = int.Parse(dr["enter_count"].ToString());//进人数
modelPDay.exit_count = int.Parse(dr["exit_count"].ToString());//出人数
new BLL.Passenger_Day_srcBLL().Add(modelPDay);
addCount++;
}
}
AddMsgItem("客流数据:新增【"+ addCount + "】条,修改【"+ updateCount + "】条");
}
}
#endregion