发现写构造器和存储过程参数太麻烦,写了个自动生成的(根据表名)
/* 文件头
文件名:TableTask.cs,WebconfigSetTask
存储路径:SqlServerTask
作者:Far.v
建立日期及时间:2008-04-26 9:20
最后修改日期及时间:2008-05-01 11:30
文件功能描述:业务逻辑层
备注:
*/
namespace SqlServerTask
{
using System;
using System.Data;
using System.Data.SqlClient;
public class TableTask
{
private string Parm_Table = "@table";
private string pope;
public string ColattStr(string popestr)
{
switch (popestr)
{
case "167":
return "VarChar";
case "56":
return "Int";
case "48":
return "TinyInt";
case "104":
return "Bit";
case "61":
return "DateTime";
case "52":
return "SmallInt";
case "127":
return "BigInt";
case "62":
return "Float";
case "173":
return "Binary";
case "34":
return "Image";
case "60":
return "Money";
case "239":
return "NChar";
case "99":
return "NText";
case "106":
return "Decimal";
case "108":
return "Decimal";
case "231":
return "NVarChar";
case "59":
return "Real";
case "58":
return "SmallDateTime";
case "122":
return "SmallMoney";
case "35":
return "Text";
case "189":
return "Timestamp";
case "36":
return "UniqueIdentifier";
case "165":
return "VarBinary";
case "175":
return "Char";
}
this.pope = this.pope + "哇!!!你要找的东西我这没有也";
return "";
}
/// <summary>
/// 自动生成 cmdParms[x] = new SqlParameter("@xxxx", SqlDbType.xxxx, xxxx);
// cmdParms[x].Value = xxxx.xxxx;
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public string selTableCmdParm(string table, string classes,bool b)
{
string cmdText = "select sc.name,sc.xtype,sc.length,sc.Name from syscolumns sc,sysobjects sb where sc.id = sb.id and sb.name = '" + table + "'";
SqlParameter[] cmdParms = new SqlParameter[1];
cmdParms[0] = new SqlParameter(Parm_Table, SqlDbType.VarChar, 50);
cmdParms[0].Value = table;
SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnStr, CommandType.Text, cmdText, cmdParms);
int num = -1;
int a = 0;
while (reader.Read())
{
if (b) //为真表示不加载ID列
{
if (reader[1].ToString() == "231")
{ this.pope = string.Concat(new object[] { this.pope, "cmdParms[", ++num, "]=new SqlParameter(Parm_", reader[3].ToString(), ",SqlDbType.", this.ColattStr(reader[1].ToString()), ",", Convert.ToInt32(reader[2].ToString()) / 2, ")<br>cmdParms[", num, "].value=" + classes + ".", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), "<br><br>" }); }
else
{ this.pope = string.Concat(new object[] { this.pope, "cmdParms[", ++num, "]=new SqlParameter(Parm_", reader[3].ToString(), ",SqlDbType.", this.ColattStr(reader[1].ToString()), ",", reader[2].ToString(), ")<br>cmdParms[", num, "].value=" + classes + ".", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), "<br><br>" }); }
if (a == 0) //运行的第一次,清空Pope 和 Num
{
this.pope = "";
a++;
num--;
}
}
else
{
if (reader[1].ToString() == "231")
{ this.pope = string.Concat(new object[] { this.pope, "cmdParms[", ++num, "]=new SqlParameter(Parm_", reader[3].ToString(), ",SqlDbType.", this.ColattStr(reader[1].ToString()), ",", Convert.ToInt32(reader[2].ToString()) / 2, ")<br>cmdParms[", num, "].value=" + classes + ".", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), "<br><br>" }); }
else
{ this.pope = string.Concat(new object[] { this.pope, "cmdParms[", ++num, "]=new SqlParameter(Parm_", reader[3].ToString(), ",SqlDbType.", this.ColattStr(reader[1].ToString()), ",", reader[2].ToString(), ")<br>cmdParms[", num, "].value=" + classes + ".", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), "<br><br>" }); }
}
//this.pope = string.Concat(new object[] { this.pope, "cmdParms[", ++num, "]=new SqlParameter(Parm_", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), ",SqlDbType.", this.ColattStr(reader[1].ToString()), ",", reader[2].ToString(), ")<br>cmdParms[", num, "].value=xxxx.", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), "<br><br>" });
}
return this.pope;
}
/// <summary>
///
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public string selTableCmdParm(string table,bool b)
{
string cmdText = "select sc.name,sc.xtype,sc.length,sc.Name from syscolumns sc,sysobjects sb where sc.id = sb.id and sb.name = '" + table + "'";
SqlParameter[] cmdParms = new SqlParameter[1];
cmdParms[0] = new SqlParameter(Parm_Table, SqlDbType.VarChar, 50);
cmdParms[0].Value = table;
SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnStr, CommandType.Text, cmdText, cmdParms);
int num = -1;
int a = 0;
while (reader.Read())
{
if (b)
{
if (reader[1].ToString() == "231")
{
this.pope = string.Concat(new object[] { this.pope, "cmdParms[", ++num, "]=new SqlParameter(Parm_", reader[3].ToString(), ",SqlDbType.", this.ColattStr(reader[1].ToString()), ",", Convert.ToInt32(reader[2].ToString()) / 2, ")<br>cmdParms[", num, "].value=xxxx.", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), "<br><br>" });
}
else
{
this.pope = string.Concat(new object[] { this.pope, "cmdParms[", ++num, "]=new SqlParameter(Parm_", reader[3].ToString(), ",SqlDbType.", this.ColattStr(reader[1].ToString()), ",", reader[2].ToString(), ")<br>cmdParms[", num, "].value=xxxx.", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), "<br><br>" });
}
if (a == 0) //运行的第一次,清空Pope 和 Num
{
this.pope = "";
a++;
num--;
}
}
else
{
if (reader[1].ToString() == "231")
{
this.pope = string.Concat(new object[] { this.pope, "cmdParms[", ++num, "]=new SqlParameter(Parm_", reader[3].ToString(), ",SqlDbType.", this.ColattStr(reader[1].ToString()), ",", Convert.ToInt32(reader[2].ToString()) / 2, ")<br>cmdParms[", num, "].value=xxxx.", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), "<br><br>" });
}
else
{
this.pope = string.Concat(new object[] { this.pope, "cmdParms[", ++num, "]=new SqlParameter(Parm_", reader[3].ToString(), ",SqlDbType.", this.ColattStr(reader[1].ToString()), ",", reader[2].ToString(), ")<br>cmdParms[", num, "].value=xxxx.", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), "<br><br>" });
}
}
//this.pope = string.Concat(new object[] { this.pope, "cmdParms[", ++num, "]=new SqlParameter(Parm_", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), ",SqlDbType.", this.ColattStr(reader[1].ToString()), ",", reader[2].ToString(), ")<br>cmdParms[", num, "].value=xxxx.", reader[0].ToString().Substring(0, 1).ToUpper(), reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower(), "<br><br>" });
}
return this.pope;
}
/// <summary>
/// 自动生成getset
/// public string getset
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public string selTableCreateAtt(string table)
{
string cmdText = "select sc.name,sc.xtype from syscolumns sc,sysobjects sb where sc.id = sb.id and sb.name='"+table+"'" ;
SqlParameter[] cmdParms = new SqlParameter[1];
cmdParms[0] = new SqlParameter(Parm_Table, SqlDbType.VarChar, 50);
cmdParms[0].Value = table;
SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnStr, CommandType.Text, cmdText, cmdParms);
this.pope = "Public";
while (reader.Read())
{
switch (reader[1].ToString())
{
case "167":
this.pope = this.pope + " string " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "56":
this.pope = this.pope + " int " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "48":
this.pope = this.pope + " byte " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "104":
this.pope = this.pope + " bool " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "61":
this.pope = this.pope + " DateTime " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "52":
this.pope = this.pope + " Int16 " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "127":
this.pope = this.pope + " Int64 " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "62":
this.pope = this.pope + " float " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "173":
this.pope = this.pope + " Byte[] " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "34":
this.pope = this.pope + " Byte[] " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "60":
this.pope = this.pope + " Decimal " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "239":
this.pope = this.pope + " string " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "99":
this.pope = this.pope + " string " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "106":
this.pope = this.pope + " Decimal " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "108":
this.pope = this.pope + " Decimal " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "231":
this.pope = this.pope + " string " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "59":
this.pope = this.pope + " Single " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "58":
this.pope = this.pope + " DateTime " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "122":
this.pope = this.pope + " Decimal " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "35":
this.pope = this.pope + " string " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "189":
this.pope = this.pope + " DateTime " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "36":
this.pope = this.pope + " Guid " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "165":
this.pope = this.pope + " Byte[] " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
case "175":
this.pope = this.pope + " string " + reader[0].ToString() + "<br>{<br>set<br>{ this.p._" + reader[0].ToString() + "=value;}<br>get<br>{return this.p_" + reader[0].ToString() + ";}<br>}<br><br>Public";
break;
default:
this.pope = this.pope + "哇!!!你要找的东西我这没有也\x00b7\x00b7";
break;
}
}
return this.pope;
}
/// <summary>
/// 自动生成private xxxx Parm_xxxx = "@xxxx";
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public string selTableParm_xxx(string table)
{
string cmdText = "select sc.name,sc.xtype,sc.Name from syscolumns sc,sysobjects sb where sc.id = sb.id and sb.name='" + table + "'";
SqlParameter[] cmdParms = new SqlParameter[1];
cmdParms[0] = new SqlParameter(Parm_Table, SqlDbType.VarChar, 50);
cmdParms[0].Value = table;
SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnStr, CommandType.Text, cmdText, cmdParms);
this.pope = "private";
while (reader.Read())
{
//this.pope = this.pope + " " +"string" +" Pram_" + reader[0].ToString().Substring(0, 1).ToUpper() + reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower() + "=\"@" + reader[0].ToString().Substring(0, 1).ToUpper() + reader[0].ToString().Substring(1, reader[0].ToString().Length - 1).ToLower() + "\" ;<br>private";
this.pope = this.pope + " " + "string" + " Pram_" + reader[2].ToString() + "=\"@" + reader[2].ToString() + "\" ;<br>private";
}
return this.pope;
}
}
}
来源:https://www.cnblogs.com/Far/archive/2008/05/06/1185342.html