public IEnumerable <SecurityModel> GetDoorActivity(DateTime from, DateTime to, int doorId) { Sqlconn.Open(); SqlCommand cmd = new SqlCommand(@"select ah.AccessHistoryId,ah.AttemptDate,e.EmployeeId, e.FirstName,e.LastName,ah.Result from AccessHistory ah join Employee e on ah.EmployeeId = e.EmployeeId where ah.DoorId=@doorId", Sqlconn); cmd.Parameters.AddWithValue("@From", from); cmd.Parameters.AddWithValue("@To", to); cmd.Parameters.AddWithValue("@doorId", doorId); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { yield return(new SecurityModel { AccessHistoryId = reader.GetInt32(0), AccessAttempt = reader.GetDateTime(1), EmployeeId = reader.GetInt32(2), FirstName = reader.GetString(3), LastName = reader.GetString(4), Result = reader.GetBoolean(5), }); } Sqlconn.Close(); }
private IEnumerable <EmployeeModel> ReadEmployeeWithDataTable(string predicate, params SqlParameter[] parameters) { DataTable table = new DataTable(); Sqlconn.Open(); var cmd = new SqlCommand("select * from Employee e " + predicate, Sqlconn); foreach (var item in parameters) { cmd.Parameters.Add(item); } SqlDataAdapter Adapter = new SqlDataAdapter(cmd); Adapter.Fill(table); for (int i = 0; i < table.Rows.Count; i++) { yield return(new EmployeeModel { EmployeeId = table.Rows[i].Field <int>("EmployeeId"), FirstName = table.Rows[i].Field <string>("FirstName"), LastName = table.Rows[i].Field <string>("LastName") }); } Sqlconn.Close(); }
public void DeleteEmployee(int Id) { Sqlconn.Open(); SqlCommand cmd = new SqlCommand("delete Employee Where EmployeeId=@Pk", Sqlconn); cmd.Parameters.AddWithValue("@Pk", Id); cmd.ExecuteNonQuery(); Sqlconn.Close(); }
public override void Show(Object x) { var data = new FlowLayoutPanel(); data = (FlowLayoutPanel)x; var conn = Sqlconn.CreateConnection(); conn.Open(); //Lấy số lượng Sản phẩm để hiển thị string sqlSelect = "SELECT MaSP FROM SanPham"; var tmp = (SqlCommand)Sqlconn.CreateCommand(sqlSelect, conn); SqlDataReader dr = tmp.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(dr); string[] MaSP = new string[(int)dt.Rows.Count]; for (int i = 0; i < (int)dt.Rows.Count; i++) { MaSP[i] = dt.Rows[i][0].ToString(); } for (int i = 0; i < MaSP.Count(); i++) { string StrSelect = "SELECT ImageSP, DonGia FROM SanPham WHERE MaSP=@MaSP"; var cmd = (SqlCommand)Sqlconn.CreateCommand(StrSelect, conn); cmd.Parameters.AddWithValue("@MaSP", MaSP[i]); SqlDataReader drr = cmd.ExecuteReader(); DataTable t = new DataTable(); t.Load(drr); var price = new Label(); price.Text = t.Rows[0][1].ToString() + " VNĐ"; price.Height = 10; price.ForeColor = Color.Blue; // var ID = new Label(); ID.Text = MaSP[i]; ID.Dock = DockStyle.Bottom; ID.BackColor = Color.AliceBlue; ID.ForeColor = Color.Red; ID.TextAlign = ContentAlignment.MiddleCenter; // byte[] b = (byte[])cmd.ExecuteScalar(); var pic = new PictureBox(); pic.Width = 100; pic.Height = 130; pic.BackgroundImageLayout = ImageLayout.Stretch; pic.BorderStyle = BorderStyle.FixedSingle; pic.Controls.Add(price); pic.Controls.Add(ID); MemoryStream ms = new MemoryStream(b); pic.BackgroundImage = Image.FromStream(ms); data.Controls.Add(pic); ID.Click += new System.EventHandler(this.picClick); } dr.Close(); conn.Close(); }
public void AddCredential(SecurityModel model) { Sqlconn.Open(); SqlCommand cmd = new SqlCommand("insert SecurityDevice(SecurityDeviceType)values(@DeviceType);select scope_identity()", Sqlconn); cmd.Parameters.AddWithValue("@DeviceType", model.Credential); cmd.ExecuteNonQuery(); Sqlconn.Close(); }
public static string GetServerTimeString() { StringBuilder strSql = new StringBuilder(); strSql.Append("select GetDate() as serverdate "); Sqlconn theTestDb = new Sqlconn(); DataSet theDs = theTestDb.Query(strSql.ToString()); return(theDs != null ? theDs.Tables[0].Rows[0][0].ToString() : null); }
public string GetCachedTree() { lock (Sqlconn) { var ser = new JavaScriptSerializer(); try { string ConfStr = ser.Serialize(conf); string sqlquery = string.Format("Select SavedTreeJson from SavedTree where Configuration='{0}'", ConfStr); Sqlconn.Open(); DataTable dtres = new DataTable(); using (SqlCommand comm = new SqlCommand(sqlquery, Sqlconn)) { using (SqlDataAdapter da = new SqlDataAdapter(comm)) { da.Fill(dtres); } } if (dtres == null || dtres.Rows.Count == 0 || dtres.Rows[0][0] == null) { return(null); } string JsonTree = dtres.Rows[0][0].ToString(); //ISdmxObjects ret = GetSdmxOBJ(dtres.Rows[0][0].ToString()); try { string sqlupd = string.Format("Update SavedTree set LastRequest='{1}' where Configuration='{0}'", ConfStr, DateTime.Now.ToString("yyyyMMdd HHmm")); using (SqlCommand commupd = new SqlCommand(sqlupd, Sqlconn)) commupd.ExecuteNonQuery(); } catch (Exception) { return(null); } return(JsonTree); } catch (Exception) { return(null); } finally { Sqlconn.Close(); } } }
public override void Show(Object x) { var data = new DataGridView(); data = (DataGridView)x; var conn = Sqlconn.CreateConnection(); conn.Open(); string sqlSelect = "SELECT MaNV as 'ID', TenNV as 'Name', GioiTinh as 'Gender',NgaySinh as 'Birthday' , Email as 'Mail' , SDT as 'Phone', DiaChi as 'Address', ViTri as 'Position' FROM NhanVien"; var cmd = (SqlCommand)Sqlconn.CreateCommand(sqlSelect, conn); SqlDataReader dr = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(dr); data.DataSource = dt; }
public static DateTime GetServerTime() { StringBuilder strSql = new StringBuilder(); strSql.Append("select GetDate() as serverdate "); Sqlconn theTestDb = new Sqlconn(); DataSet theDs = theTestDb.Query(strSql.ToString()); if (theDs != null && theDs.Tables.Count != 0) { return(DateTime.Parse(theDs.Tables[0].Rows[0][0].ToString())); } else { return(DateTime.Now); } }
public void CreateEmployee(EmployeeModel model) { Sqlconn.Open(); // SqlCommand cmd2 = new SqlCommand(@"if exists(select * from Employee e where e.EmployeeId=@Pk) //begin //update Employee //set FirstName = @FName, //LastName = @LName where EmployeeId = @Pk //end //else // begin // insert Employee(FirstName, LastName)values(@FName, @LName) //select scope_identity() //end"); SqlCommand cmd = new SqlCommand("insert Employee(FirstName,LastName)values(@FName,@LName)", Sqlconn); cmd.Parameters.AddWithValue("@Fname", model.FirstName); cmd.Parameters.AddWithValue("@LName", model.LastName); // cmd.Parameters.AddWithValue("@Pk", model.EmployeeId); cmd.ExecuteNonQuery(); Sqlconn.Close(); }
private IEnumerable <EmployeeModel> ReadEmployee(string predicate, params SqlParameter[] parameters) { Sqlconn.Open(); var cmd = new SqlCommand("select * from Employee e " + predicate, Sqlconn); foreach (var item in parameters) { cmd.Parameters.Add(item); } SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { yield return(new EmployeeModel { EmployeeId = reader.GetInt32(0), FirstName = reader.GetString(1), LastName = reader.GetString(2) }); } Sqlconn.Close(); }
//private IEnumerable<SecurityModel> GetUnseccessfulAttempts(DateTime from, DateTime to) //{ // SecurityModel model = new SecurityModel(); // Sqlconn.Open(); // SqlCommand cmd = new SqlCommand("select * from AccessHistory ah where ah.AttemptDate >=from and ah.AttemptDate <= to and ah.Result=0",Sqlconn); // cmd.Parameters.AddWithValue("@from", model.AccessAttempt); // cmd.Parameters.AddWithValue("@to", model.AccessAttempt); // SqlDataReader reader = cmd.ExecuteReader(); // while (reader.Read()) // { // yield return new SecurityModel // { // AccessHistoryId = reader.GetInt32(0), // AccessAttempt = reader.GetDateTime(1), // Result = reader.GetBoolean(2) // }; // } //} //private IEnumerable<SecurityModel> GetSuccessfulAttempts2(DateTime from, DateTime to) //{ // SecurityModel model = new SecurityModel(); // Sqlconn.Open(); // SqlCommand cmd = new SqlCommand("select * from AccessHistory ah where ah.AttemptDate >=from and ah.AttemptDate <= to and ah.Result=1",Sqlconn); // cmd.Parameters.AddWithValue("@from", model.AccessAttempt); // cmd.Parameters.AddWithValue("@to", model.AccessAttempt); // SqlDataReader reader = cmd.ExecuteReader(); // while (reader.Read()) // { // yield return new SecurityModel // { // AccessHistoryId = reader.GetInt32(0), // AccessAttempt = reader.GetDateTime(1), // Result = reader.GetBoolean(2) // }; // } //} //public IEnumerable<SecurityModel> GetSuspiciousAttempts(DateTime from, DateTime to) //{ // SecurityModel model = new SecurityModel(); // var Query1 = GetUnseccessfulAttempts(from, to); // var Query2 = GetSuccessfulAttempts2(from, to); // foreach (var failure in Query1) // { // foreach (var success in Query2) // { // if(failure.EmployeeId==success.EmployeeId&&failure.DoorId==success.DoorId&&failure.AccessAttempt<success.AccessAttempt.AddMinutes(2)) // { // yield return new SecurityModel // { // AccessHistoryId = failure.AccessHistoryId, // AccessAttempt = failure.AccessAttempt, // Door = failure.Door, // FirstName = failure.FirstName, // LastName = failure.LastName, // Result = failure.Result // }; // } // } // } //Continue this we need to compare the 2 queries and see if the 2nd 1 has a true less then 2 minutes after a false on the 1st list and exclude it from the result public IEnumerable <SecurityModel> GetSuspiciousAttempts(DateTime from, DateTime to) { SecurityModel model = new SecurityModel(); Sqlconn.Open(); SqlCommand cmd = new SqlCommand("select * from AccessHistory ah where ah.AttemptDate >=from and ah.AttemptDate <= to and ah.Result=0", Sqlconn); cmd.Parameters.AddWithValue("@from", model.AccessAttempt); cmd.Parameters.AddWithValue("@to", model.AccessAttempt); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { model.AccessHistoryId = reader.GetInt32(0); var Attempt = model.AccessAttempt = reader.GetDateTime(1); var EmpId1 = model.EmployeeId = reader.GetInt32(2); model.FirstName = reader.GetString(3); model.LastName = reader.GetString(4); var door1 = model.DoorId = reader.GetInt32(5); model.Result = reader.GetBoolean(6); var Plus2minutes = Attempt.AddMinutes(2); SqlCommand cmnd = new SqlCommand("select * from AccessHistory ah where ah.AttemptDate >=from and ah.AttemptDate <= to", Sqlconn); SqlDataReader rdr = cmnd.ExecuteReader(); while (rdr.Read()) { model.AccessHistoryId = reader.GetInt32(0); var Attempt2 = model.AccessAttempt = reader.GetDateTime(1); var EmpId2 = model.EmployeeId = reader.GetInt32(2); model.FirstName = reader.GetString(3); model.LastName = reader.GetString(4); var door2 = model.DoorId = reader.GetInt32(5); var Result = model.Result = reader.GetBoolean(6); //We also need if there is no result if (door1 == door2 && EmpId1 == EmpId2 && Attempt2 > Plus2minutes || Attempt2 < Plus2minutes && Result == false) { } } } }
public void ThreadSaveCachedTree(object TObject) { string JsonTree = (string)TObject; lock (Sqlconn) { var ser = new JavaScriptSerializer(); try { Sqlconn.Open(); string ConfStr = ser.Serialize(conf); //string TreeName = string.Format("tree_{0}.json", Guid.NewGuid()); //SaveSdmxOBJ(SdmxOBJ, TreeName); string sqlcount = string.Format("Select count(*) from SavedTree where Configuration='{0}'", ConfStr.Replace("'", "''")); int conta = 0; using (SqlCommand commconta = new SqlCommand(sqlcount, Sqlconn)) conta = Convert.ToInt32(commconta.ExecuteScalar()); if (conta == 0) { string sqlupd = string.Format("INSERT INTO SavedTree (Configuration, SavedTreeJson, LastUpdate, LastRequest) VALUES ('{0}', '{1}', '{2}', '{2}')" , ConfStr.Replace("'", "''"), JsonTree.Replace("'", "''"), DateTime.Now.ToString("yyyyMMdd HHmm")); using (SqlCommand commupd = new SqlCommand(sqlupd, Sqlconn)) commupd.ExecuteNonQuery(); } } catch (Exception) { return; } finally { Sqlconn.Close(); } } }