You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

356 lines
12 KiB

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Gm_WMS.DataAccess.DataService;
namespace Stone.Entity
{
public class EntityBase : IDisposable
{
public string TableName = string.Empty;
public DataTable Table;
public LocalDBService db;
public string sql = string.Empty;
#region 构造函数和初始化代码
public EntityBase()
{
}
public void Init()
{
if (db == null)
{
db = new LocalDBService();
}
Table = new DataTable();
Table.TableName = TableName;
//初始化时执行一个SQL语句,用于获取Table的结构
sql = "select top 0 * from " + TableName;
Table = db.Exec_DataSet(sql).Tables[0];
}
#endregion
#region 获取数据
public virtual DataSet GetData()
{
sql = "select * from " + TableName;
return db.Exec_DataSet(sql);
}
public virtual DataSet GetData(string strwhere)
{
sql = "select * from " + TableName + " where " + strwhere;
return db.Exec_DataSet(sql);
}
public virtual DataSet GetData(string fields, int ID)
{
if (fields == "") fields = "*";
sql = "select " + fields + " from " + TableName + " where id=" + ID;
return db.Exec_DataSet(sql);
}
public virtual DataSet GetData(string fields, string orderby)
{
if (fields == "") fields = "*";
sql = "select " + fields + " from " + TableName + " order by " + orderby;
return db.Exec_DataSet(sql);
}
public virtual DataSet GetData(string fields, string strwhere, string orderby)
{
if (fields == "") fields = "*";
sql = "select " + fields + " from " + TableName + " where " + strwhere + " order by " + orderby;
return db.Exec_DataSet(sql);
}
public virtual DataSet GetDataDistinct(string fields, string strwhere, string orderby)
{
sql = "select Distinct " + fields + " from " + TableName + " where " + strwhere + " order by " + orderby;
return db.Exec_DataSet(sql);
}
#endregion
#region 获取分页数据
public virtual DataSet GetDataLimit(int rows1, int rows2)
{
// string sql = @"
// select {0} from (
// select ROW_NUMBER() over(order by {1}) as rows_number_9999999, {0} from {2} where {5}
// ) as query_temp_table_9999999 where rows_number_9999999 between {3} and {4} and ({5})
// ";
string sql = @"
select {0} from (
select ROW_NUMBER() over(order by {1}) as rows_number_9999999, {0} from {2} where {5}
) as query_temp_table_9999999 where rows_number_9999999 between {3} and {4}
";
object[] obj = new object[6];
obj[0] = "*";
obj[1] = "[ID] asc";
obj[2] = TableName;
obj[3] = rows1;
obj[4] = rows2;
obj[5] = "1=1";
sql = string.Format(sql, obj);
DataSet dsData = db.Exec_DataSet(sql);
dsData.Tables[0].Columns.Remove("rows_number_9999999");
return dsData;
}
public virtual DataSet GetDataLimit(int rows1, int rows2, string strwhere)
{
// string sql = @"
// select {0} from (
// select ROW_NUMBER() over(order by {1}) as rows_number_9999999, {0} from {2} where {5}
// ) as query_temp_table_9999999 where rows_number_9999999 between {3} and {4} and ({5})
// ";
string sql = @"
select {0} from (
select ROW_NUMBER() over(order by {1}) as rows_number_9999999, {0} from {2} where {5}
) as query_temp_table_9999999 where rows_number_9999999 between {3} and {4}
";
object[] obj = new object[6];
obj[0] = "*";
obj[1] = "[ID] asc";
obj[2] = TableName;
obj[3] = rows1;
obj[4] = rows2;
obj[5] = strwhere;
sql = string.Format(sql, obj);
DataSet dsData = db.Exec_DataSet(sql);
dsData.Tables[0].Columns.Remove("rows_number_9999999");
return dsData;
}
public virtual DataSet GetDataLimit(int rows1, int rows2, string fields, string orderby)
{
if (fields == "") fields = "*";
// string sql = @"
// select {0} from (
// select ROW_NUMBER() over(order by {1}) as rows_number_9999999, {0} from {2} where {5}
// ) as query_temp_table_9999999 where rows_number_9999999 between {3} and {4} and ({5})
// ";
string sql = @"
select {0} from (
select ROW_NUMBER() over(order by {1}) as rows_number_9999999, {0} from {2} where {5}
) as query_temp_table_9999999 where rows_number_9999999 between {3} and {4}
";
object[] obj = new object[6];
obj[0] = fields;
obj[1] = orderby;
obj[2] = TableName;
obj[3] = rows1;
obj[4] = rows2;
obj[5] = "1=1";
sql = string.Format(sql, obj);
DataSet dsData = db.Exec_DataSet(sql);
dsData.Tables[0].Columns.Remove("rows_number_9999999");
return dsData;
}
public virtual DataSet GetDataLimit(int rows1, int rows2, string fields, string strwhere, string orderby)
{
if (fields == "") fields = "*";
// string sql = @"
// select {0} from (
// select ROW_NUMBER() over(order by {1}) as rows_number_9999999, {0} from {2} where {5}
// ) as query_temp_table_9999999 where rows_number_9999999 between {3} and {4} and ({5})
// ";
string sql = @"
select {0} from (
select ROW_NUMBER() over(order by {1}) as rows_number_9999999, {0} from {2} where {5}
) as query_temp_table_9999999 where rows_number_9999999 between {3} and {4}
";
object[] obj = new object[6];
obj[0] = fields;
obj[1] = orderby;
obj[2] = TableName;
obj[3] = rows1;
obj[4] = rows2;
obj[5] = strwhere;
sql = string.Format(sql, obj);
DataSet dsData = db.Exec_DataSet(sql);
dsData.Tables[0].Columns.Remove("rows_number_9999999");
return dsData;
}
#endregion
#region 获取不重复的数据
public virtual DataTable GetDistinctData(string Fields)
{
string sql = "select Distinct [" + Fields + "] from " + TableName;
return db.Exec_DataSet(sql).Tables[0];
}
#endregion
#region 增、删、修
public virtual DataRow Add(DataRow drData)
{
DataRow drResult = null;
sql = "insert into {0}({1}) values({2});select @@Identity";
string fields = "";
string values = "";
foreach (DataColumn dc in Table.Columns)
{
if (dc.ColumnName.ToUpper() != "ID")
{
if (drData[dc.ColumnName] is DBNull) continue;
fields += "[" + dc.ColumnName + "]" + ",";
if ((dc.DataType.Name == "Int32" || dc.DataType.Name == "Decimal") && drData[dc.ColumnName].ToString() == "")
{
values += "'0',";
}
else if (dc.DataType.Name == "DateTime" && drData[dc.ColumnName].ToString() == "")
{
values += "getdate(),";
}
else
{
values += "'" + drData[dc.ColumnName] + "',";
}
}
}
if (fields.Length > 1) fields = fields.Substring(0, fields.Length - 1);
if (values.Length > 1) values = values.Substring(0, values.Length - 1);
object[] o = new object[3];
o[0] = TableName;
o[1] = fields;
o[2] = values;
sql = string.Format(sql, o);
string id = db.Exec_DataSet(sql).Tables[0].Rows[0][0].ToString();
sql = "select * from " + TableName + " where id=" + id;
drResult = db.Exec_DataSet(sql).Tables[0].Rows[0];
return drResult;
}
public virtual DataRow Edit(DataRow drData)
{
DataRow drResult = null;
sql = "update {0} set {1} where id={2}";
string fields = "";
string values = "";
foreach (DataColumn dc in Table.Columns)
{
if (dc.ColumnName.ToUpper() != "ID")
{
if (drData[dc.ColumnName] is DBNull) continue;
if ((dc.DataType.Name == "Int32" || dc.DataType.Name == "Decimal") && drData[dc.ColumnName].ToString() == "")
{
values = "'0'";
}
else if (dc.DataType.Name == "DateTime" && drData[dc.ColumnName].ToString() == "")
{
values = "getdate()";
}
else
{
values = "'" + drData[dc.ColumnName] + "'";
}
fields += "[" + dc.ColumnName + "]=" + values + ",";
}
}
if (fields.Length > 1) fields = fields.Substring(0, fields.Length - 1);
object[] o = new object[3];
o[0] = TableName;
o[1] = fields;
o[2] = drData["id"];
sql = string.Format(sql, o);
db.Exec_NonQuery(sql);
string id = drData["id"].ToString();
sql = "select * from " + TableName + " where id=" + id;
drResult = db.Exec_DataSet(sql).Tables[0].Rows[0];
return drResult;
}
public virtual void Edit(string SetFields, string strWhere)
{
sql = "update {0} set {1} where {2}";
object[] o = new object[3];
o[0] = TableName;
o[1] = SetFields;
o[2] = strWhere;
sql = string.Format(sql, o);
db.Exec_NonQuery(sql);
}
public virtual void Del(DataRow drData)
{
string ID = drData["id"].ToString();
sql = "delete from " + TableName + " where ID=" + ID;
db.Exec_NonQuery(sql);
}
public virtual void Del(int ID)
{
sql = "delete from " + TableName + " where ID=" + ID;
db.Exec_NonQuery(sql);
}
public virtual void Del(string strWhere)
{
sql = "delete from " + TableName + " where " + strWhere;
db.Exec_NonQuery(sql);
}
#endregion
#region 获取系统当前时间
public DateTime GetDateTime()
{
return Convert.ToDateTime(db.Exec_DataSet("select getdate() as d").Tables[0].Rows[0]["d"]);
}
#endregion
#region 释放对象
public void Dispose()
{
if (Table != null) Table.Dispose();
}
#endregion
}
}