using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Windows.Forms; using Forks.EnterpriseServices; using Forks.Utils.Data; namespace BLUtil { public class DbUtil { public static string ConnectionStr = ""; public static object DialogReturnValue { get; set; } private static int _startTime = 12;//默认12点 private static DateTime NewToday { get { if (DateTime.Now.Hour >= _startTime) { return new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, _startTime, 0, 0); } return new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, _startTime, 0, 0).AddDays(-1); } } private static DateTime NextNewToday { get { return NewToday.AddHours(24); } } public static void Init() { ConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Directory.GetCurrentDirectory() + "\\AppDb.mdb"; } public static int GetSequence() { using (var sqlUtil = DbFactory.GetSqlUtil(ConnectionStr, DbProviderType.OleDb)) { var sql = string.Format("select top 1 Sequence from [Weight] where [DateTime] >= @P1 and [DateTime] < @P2 order by Sequence desc"); var paramList = "@P1;@P2"; var obj = sqlUtil.ExecuteScalar(sql, paramList , NewToday, NextNewToday); return obj != null ? (int.Parse(obj.ToString()) + 1) : 1; } } private static string _querySql = "SELECT [ID], Sequence, [DateTime] ,Goods_ID, Goods_Name, Goods_Code, Weight, Unit FROM [Weight] where Finish = 0 order by [ID] desc"; public static DataSet GetWeight() { using (var sqlUtil = DbFactory.GetSqlUtil(ConnectionStr, DbProviderType.OleDb)) { DataSet data = null; try { data = sqlUtil.ExecuteSql(_querySql); sqlUtil.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return data; } } public static void InsertGoods(List infos, bool clearFirst = false) { using (var sqlUtil = DbFactory.GetSqlUtil(ConnectionStr, DbProviderType.OleDb)) { if (clearFirst) { var delete = "Delete FROM Goods"; sqlUtil.ExecuteNonQuery(delete); } foreach (var obj in infos) { var sql = String.Format( "Insert into Goods([Goods_ID],[Goods_Name],[Goods_Spell],[Goods_Code],[Unit],[RowVersion]) values (@P1, @P2, @P3, @P4, @P5, @P6)"); string paramList = "@P1;@P2;@P3;@P4;@P5;@P6"; sqlUtil.ExecuteNonQuery(sql, paramList, obj.ID, obj.Name, obj.Spell, obj.Code, obj.Unit, obj.RowVersion); } sqlUtil.Commit(); } } public static List SelectGoods(string searchKey) { var infos = new List(); using (var sqlUtil = DbFactory.GetSqlUtil(ConnectionStr, DbProviderType.OleDb)) { var sql = String.Format( "Select top 30 [Goods_ID],[Goods_Name], [Goods_Code],[Unit] From Goods Where ([Goods_Name] like @P1) or ([Goods_Spell] like @2) or ([Goods_Code] like @P3) order by [Goods_ID] "); var paramList = "@P1;@P2;@P3;"; using (var reader = sqlUtil.ExecuteReader(sql, paramList, "%" + searchKey + "%", "%" + searchKey + "%", "%" + searchKey + "%")) { while (reader.Read()) { var obj = new BaseInforObj(); obj.ID = (int)reader["Goods_ID"]; obj.Name = (string)reader["Goods_Name"]; obj.Code = (string)reader["Goods_Code"]; obj.Unit = (string)reader["Unit"]; infos.Add(obj); } } } return infos; } public static List SelectStore(string searchKey) { var infos = new List(); using (var sqlUtil = DbFactory.GetSqlUtil(ConnectionStr, DbProviderType.OleDb)) { var sql = String.Format( "Select top 30 [Store_ID],[Store_Name] From [Store] Where ([Store_Name] like @P1) or ([Store_Spell] like @P2) order by [Store_ID] "); var paramList = "@P1;@P2;"; using (var reader = sqlUtil.ExecuteReader(sql, paramList, "%" + searchKey + "%", "%" + searchKey + "%")) { while (reader.Read()) { var obj = new BaseInforObj(); obj.ID = (int)reader["Store_ID"]; obj.Name = (string)reader["Store_Name"]; infos.Add(obj); } } } return infos; } public static void InsertStore(List infos) { using (var sqlUtil = DbFactory.GetSqlUtil(ConnectionStr, DbProviderType.OleDb)) { var delete = "Delete FROM Store"; sqlUtil.ExecuteNonQuery(delete); int i = 0; foreach (var obj in infos) { i++; var sql = String.Format( "Insert into Store([Store_ID],[Store_Name],[Store_Spell],[RowVersion]) values (@P1, @P2, @P3, @P4 )"); string paramList = "@P1;@P2;@P3;@P4 "; sqlUtil.ExecuteNonQuery(sql, paramList, obj.ID, obj.Name, obj.Spell, obj.RowVersion); } sqlUtil.Commit(); } } public static void InsertWeight(WeightTable bo) { using (var sqlUtil = DbFactory.GetSqlUtil(ConnectionStr, DbProviderType.OleDb)) { var sql = String.Format( "Insert into Weight([Goods_ID],[Goods_Name],[Goods_Code],[Unit],[Weight],[DateTime],[Sequence],[Finish]) values (@P1, @P2, @P3, @P4 ,@P5, @P6 , @P7,0 )"); string paramList = "@P1;@P2;@P3;@P4;@P5;@P6;@P7 "; sqlUtil.ExecuteNonQuery(sql, paramList, bo.Goods_ID, bo.Goods_Name, bo.Goods_Code, bo.Unit, bo.Weight, bo.DateTime, bo.Sequence); sqlUtil.Commit(); } } public void GetLogList(string conStr, string sql) { //using (var conn = new OleDbConnection(conStr)) { // conn.Open(); // var myCommand = new OleDbCommand(sql, conn); // using (var reader = myCommand.ExecuteReader()) { // while (reader.Read()) { // var log = new FrmMain.AttLog(); // log.EnrNo = (string)reader["EnrNo"]; // log.DateTime = (DateTime)reader["DateTime"]; // log.TyVerifype = (int)reader["Verify"]; // log.ID = (int)reader["ID"]; // log.IP = (string)reader["IP"]; // list.Add(log); // } // } // conn.Close(); //} //return list; } //public static void UpdateState(List logs) { // using (var conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + // Directory.GetCurrentDirectory() + "\\AttDB.mdb")) { // conn.Open(); // var sql = String.Format( // "update AttLog set [Uploaded]=1 where [ID] in ({0})", string.Join(",", logs.Select(x => x.ID))); // var cmd = new OleDbCommand(sql, conn); // cmd.ExecuteNonQuery(); // conn.Close(); // } //} } }