注:此页面谈到的内容
1.相关更新文章标题链接:将 Sql Server 表信息 C# 对象化 小工具
2. 实现小工具下载链接:下载
此:内容面向对象:初学人士
思源: C# 操作数据库 数据表时,可能遇到比较纠结地去应付列名,列值类型...等,[当然,以下将说明的内容也可以加入对长度的检测...], 看似累同,却又不得不键入的 Sql 语句,也会经常遇到 Sql 数据表结果,列名,列数据类型的更改 ,从而带来可能的错误...
因此: 面对上述情况,慢慢形成将 数据表 相关信息 生成 C# 类化,以达到基本的 C# 程序中 对 数据表 操作的 相关需求,解决上述问题
表信息类化信息简述:以表名生成类名,以列名生成列名参数名[用于 DataTable 等相关操作],以 [g列名] 生成 列名赋值与取值 参数,相关列信息数据数组,相关取值与赋值函数
优点:当对数据表列信息进行更改后,再重新生成此类溶合于代码中,即可方便地找出相关因数据表改变而造成的不同的错误...可能已更改的列名,可能已取消的列名,可能已改变的数值类型...等等
缺点:当然要稍多费点内存和 Cpu
以下是相关 表 信息类,其它相关辅助 函数等 都在 上面 下载链接 小工具中
/// <summary>
/// 表名 生成的 类名
/// </summary>
public partial class Csp
{
#region Csp
/// <summary>
/// Csp
/// </summary>
public static string Tb = "Csp";
public Csp()
{
gVluS = new object[] { gIdx, gNam, gDt };
SetVluS = new Fun.GVluHdler[] { GIdx, GNam, GDt };
mak = F.DB.Mdf.Sql.Crt.TbSql(Tb, colS, typS, sizS, vluS, keyS, incS, unqS, nulS, F.DB.ETyp.lit);
}
#region 列名枚举
/// <summary>
/// [自增列]
/// </summary>
public static string idx = "idx";
/// <summary>
///
/// </summary>
public static string nam = "nam";
/// <summary>
/// 日期
/// </summary>
public static string dt = "dt";
#endregion
#region 列值获取变量枚举
/// <summary>
/// [自增列] bigint
/// </summary>
public long gIdx;
public string gIdxs;
/// <summary>
/// nchar
/// </summary>
public string gNam;
/// <summary>
/// 日期 datetime
/// </summary>
public string gDt;
#endregion
#region 列值长度变量枚举
/// <summary>
/// nam 列长度: 10
/// </summary>
public static int lNam = 10;
#endregion
#region 建表相关数据组列举
/// <summary>
/// "idx + c.d + nam + c.d + dt"
/// </summary>
public static string cols = idx + c.d + nam + c.d + dt;
/// <summary>
/// {F.DB.Mdf.typ.bi,F.DB.Mdf.typ.nc,F.DB.Mdf.typ.dt}
/// </summary>
public static string[] typS = new string[] { F.DB.Mdf.typ.bi, F.DB.Mdf.typ.nc, F.DB.Mdf.typ.dt };
/// <summary>
/// {0,10,0}
/// </summary>
public static int[] sizS = new int[] { 0, 10, 0 };
/// <summary>
/// {null,null,"getdate()"}
/// </summary>
public static object[] vluS = new object[] { null, null, "getdate()" };
/// <summary>
/// {true,false,false}
/// </summary>
public static bool[] keyS = new bool[] { true, false, false };
/// <summary>
/// {"1,1","",""}
/// </summary>
public static string[] incS = new string[] { "1,1", "", "" };
/// <summary>
/// {false,false,false}
/// </summary>
public static bool[] unqS = new bool[] { false, false, false };
/// <summary>
/// {false,false,false}
/// </summary>
public static bool[] nulS = new bool[] { false, false, false };
/// <summary>
/// {"[自增列] ","","日期"}
/// </summary>
public static string[] expS = new string[] { "[自增列] ", "", "日期" };
/// <summary>
/// {空值组 [用于特殊空行添加等]:"","",""}
/// </summary>
public static string[] empS = new string[] { "", "", "" };
/// <summary>
/// "idx + c.d + nam + c.d + dt"
/// </summary>
public static string[] colS = cols.Split(c.d);
/// <summary>
/// "idx + c.d + nam + c.d + dt"
/// </summary>
public static List<string> colL = new List<string>(colS);
/// <summary>
/// {gIdx,gNam,gDt}
/// </summary>
public static object[] gVluS;
/// <summary>
/// 此用于存放在与列名相对应的 赋值函数 数组中,以通过列序,调用列对应的赋值函数 赋出列值
/// </summary>
public Fun.GVluHdler[] SetVluS;
/// <summary>
/// "nam + c.d + dt"
/// </summary>
public static string _incs = nam + c.d + dt;
/// <summary>
/// "nam + c.d + dt"
/// </summary>
public static string msts = nam + c.d + dt;
/// <summary>
/// ""
/// </summary>
public static string unqs = "";
/// <summary>
/// "nam"
/// </summary>
public static string _defs = nam;
/// <summary>
/// "nam"
/// </summary>
public static string _funs = nam;
#endregion
#region 特殊用途函数及变量
/// <summary>
/// 存放 列名组 各列索引 对应 综合列名组中 相同的列名 索引 组
/// </summary>
public static int[] viwIdxS = new int[0];
/// <summary>
/// 获取 列名组 各列索引 对应 综合列名组中 相同的列名 索引 组
/// </summary>
public static void ViwIdxS(string[] itmColS)
{
if (viwIdxS.Length > 0)
return;
viwIdxS = F.Ary.Asy.MchItmIdxS(itmColS, colL);
}
/// <summary>
/// 获取 列名组 各列索引 对应 综合列名组中 相同的列名 索引 组
/// </summary>
public static void ViwIdxS(string[] itmColS, string[] likColS, List<string> viwColL)
{
if (viwIdxS.Length == viwColL.Count)
return;
viwIdxS = F.Ary.Asy.MchItmIdxS(itmColS, likColS, viwColL, colL);
}
/// <summary>
/// 获取 需要按列名 对应 显示在 综合列名组中 相同的列名 列值组
/// </summary>
public static string[] ColVluS;
/// <summary>
/// 列名组 各列值 对应 综合列名组中 相同的列名
/// </summary>
public static string[] ViwVluS = Fun.MchItmIdxVluS(viwIdxS, ColVluS);
/// <summary>
/// 专用于 GetVluS 函数操作
/// </summary>
static int vluInTyp = -1;
/// <summary>
/// 将 string[] 数组值 赋值组对应序号的列名
/// </summary>
public void GetVluS(string[] setS)
{
vluInTyp = Fun.ETypi.strS;
GIdx(setS);
GNam(setS);
GDt(setS);
vluInTyp = -1;
}
/// <summary>
/// 将 DataRow 行列值 赋值组对应序号的列名
/// </summary>
public void GetVluS(DataRow dr)
{
vluInTyp = Fun.ETypi.dr;
GIdx(dr);
GNam(dr);
GDt(dr);
vluInTyp = -1;
}
/// <summary>
/// 将 ListViewItem 行列值 赋值组对应序号的列名
/// </summary>
public void GetVluS(ListViewItem lvi)
{
vluInTyp = Fun.ETypi.lvi;
GIdx(lvi);
GNam(lvi);
GDt(lvi);
vluInTyp = -1;
}
/// <summary>
/// 将 DataGridViewRow 行列值 赋值组对应序号的列名
/// </summary>
public void GetVluS(DataGridViewRow dgvr)
{
vluInTyp = Fun.ETypi.dgvr;
GIdx(dgvr);
GNam(dgvr);
GDt(dgvr);
vluInTyp = -1;
}
#endregion
#region 相关数据相关方式赋值函数
/// <summary>
/// 通过给出的列序,调用与列名对应的 SetVluS 赋值函数数组,为 Csp 表对象的 gIdx 参数赋值
/// </summary>
public void GIdx(object vlu)
{
Fun.GVlu(colL, idx, vlu, vluInTyp, ref gIdxs);
gIdx = long.Parse(gIdxs);
}
/// <summary>
/// 通过给出的列序,调用与列名对应的 SetVluS 赋值函数数组,为 Csp 表对象的 gNam 参数赋值
/// </summary>
public void GNam(object vlu)
{
Fun.GVlu(colL, nam, vlu, vluInTyp, ref gNam);
}
/// <summary>
/// 通过给出的列序,调用与列名对应的 SetVluS 赋值函数数组,为 Csp 表对象的 gDt 参数赋值
/// </summary>
public void GDt(object vlu)
{
Fun.GVlu(colL, dt, vlu, vluInTyp, ref gDts);
gDt = DateTime.Parse(gDts);
}
/// <summary>
/// 根据所选列名组,返回对应的列值组
/// </summary>
public static bool SltVluS(List<string> sltColL, out object[] sltVluS)
{
return Fun.SltVluS(colS, gVluS, sltColL, out sltVluS);
}
#endregion
#region Vlus,VluS,StrS
/// <summary>
/// {返回当表类 行 所有列 值分隔串 : gIdx+_+gNam+_+gDt}
/// </summary>
public string Vlus(string _)
{
return gIdx + _ + gNam + _ + gDt;
}
/// <summary>
/// {返回当表类 行 所有列 值组 : gIdx,gNam,gDt}
/// </summary>
public object[] VluS()
{
return new object[] { gIdx, gNam, gDt };
}
/// <summary>
/// {返回当表类 行 所有列 值组 : gIdx+"",gNam+"",gDt+""}
/// </summary>
public string[] StrS()
{
return new string[] { gIdx + "", gNam + "", gDt + "" };
}
#endregion
#region 表相关插入语句生成函数列举
/// <summary>
/// "nam"
/// </summary>
public string Ins_Func(string gNam)
{
string sql = "insert into Csp(" + nam + ") values(" + "'" + gNam + "'" + ");";
return sql;
}
/// <summary>
/// "nam"
/// </summary>
public string Ins_Func()
{
string sql = "insert into Csp(" + nam + ") values(" + "'" + gNam + "'" + ");";
return sql;
}
/// <summary>
/// {返回当表类 行 非自增列,非函数列 值组 : gNam}
/// </summary>
public object[] Vlu_FunS()
{
return new object[] { gNam };
}
public string cmdSql;
#endregion
#region 建表 SQL
/// <summary>
/// (Csp.nam, colS, typS, sizS, vluS, keyS, incS, unqS, nulS, F.DB.ETyp.lit);
/// </summary>
public static string mak;
#endregion
#region C# 建表 SQL
public static string Sql = "create table Csp("
+ "idx bigint primary key AUTOINCREMENT,"
+ "nam nchar(10) not null,"
+ "dt datetime default(getdate())"
+ ");";
#endregion
#endregion
}
交流 QQ : 2412366909@qq.com
手机号码:177-7499-4428
浙公网安备 33010602011771号