|
|
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<BaseInforObj> 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<BaseInforObj> SelectGoods(string searchKey) {
|
|
|
var infos = new List<BaseInforObj>();
|
|
|
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<BaseInforObj> SelectStore(string searchKey) {
|
|
|
var infos = new List<BaseInforObj>();
|
|
|
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<BaseInforObj> 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 static void UpdateState( ) {
|
|
|
using (var sqlUtil = DbFactory.GetSqlUtil(ConnectionStr, DbProviderType.OleDb)) {
|
|
|
var sql = String.Format(
|
|
|
"Update Weight set [Finish] = 1 where [Finish]=0");
|
|
|
sqlUtil.ExecuteNonQuery(sql );
|
|
|
sqlUtil.Commit();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static void Delete(object id ) {
|
|
|
using (var sqlUtil = DbFactory.GetSqlUtil(ConnectionStr, DbProviderType.OleDb)) {
|
|
|
var sql = String.Format("delete FROM [Weight] where id = {0}", id);
|
|
|
sqlUtil.ExecuteNonQuery(sql );
|
|
|
sqlUtil.Commit();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|