首页 > 编程 > .NET > 正文

dotnet代码自动生成机的实现

2024-07-21 02:23:38
字体:
来源:转载
供稿:网友
在我们写程序的时候,特别是数据库应用程序的时候,经常会遇到这样的情况:对于一个给定的表,写出这个表对应的类(用一句时髦的话说是实现业务实体类),类的数据成员是所有的字段,并且类含有该表的添加修改删除等操作。还有,对于一个给定的存储过程,要完成根据存储过程存取数据或别的数据库操作。如下代码就是我们通常要完成的:
1.表的业务实体化
private int iid ;
public int id
{
get
{
return iid ;
}
set
{
iid = value ;
}
}


private string strname ;
public string name
{
get
{
return strname ;
}
set
{
strname = value ;
}
}


private string strcode ;
public string code
{
get
{
return strcode ;
}
set
{
strcode = value ;
}
}


private string strdescription ;
public string description
{
get
{
return strdescription ;
}
set
{
strdescription = value ;
}
}


private int ifatherid ;
public int fatherid
{
get
{
return ifatherid ;
}
set
{
ifatherid = value ;
}
}


private int itype ;
public int type
{
get
{
return itype ;
}
set
{
itype = value ;
}
}


private int iuserid ;
public int userid
{
get
{
return iuserid ;
}
set
{
iuserid = value ;
}
}


ublic bool add()
{
sqlconnection conn = sqlconn.instance().connection ;

string strsql = "insert into book(id, name, code, description, fatherid, type, userid)"
+"values(@id, @name, @code, @description, @fatherid, @type, @userid)" ;

sqlcommand command = new sqlcommand(strsql,conn) ;

command.parameters.add("@id",sqldbtype.int ) ;
command.parameters["@id"].value = iid ;

command.parameters.add("@name",sqldbtype.nvarchar ,50) ;
if (strname!= null )
command.parameters["@name"].value = strname ;
else
command.parameters["@name"].value = dbnull.value ;

command.parameters.add("@code",sqldbtype.nvarchar ,255) ;
if (strcode!= null )
command.parameters["@code"].value = strcode ;
else
command.parameters["@code"].value = dbnull.value ;

command.parameters.add("@description",sqldbtype.nvarchar ,255) ;
if (strdescription!= null )
command.parameters["@description"].value = strdescription ;
else
command.parameters["@description"].value = dbnull.value ;

command.parameters.add("@fatherid",sqldbtype.int ) ;
command.parameters["@fatherid"].value = ifatherid ;

command.parameters.add("@type",sqldbtype.int ) ;
command.parameters["@type"].value = itype ;

command.parameters.add("@userid",sqldbtype.int ) ;
command.parameters["@userid"].value = iuserid ;

try
{
conn.open() ;
command.executenonquery() ;
return true ;
}
catch(exception e)
{
throw(new exception("error in the database"+e.message)) ;
}
finally
{
conn.close() ;
}
}
public bool modify()
{
sqlconnection conn = sqlconn.instance().connection ;
string strsql ="update book set id = @id, name = @name, code = @code, description = @description, fatherid = @fatherid, type = @type, userid = @userid "
+ " where id [email protected] " ;
sqlcommand command = new sqlcommand(strsql,conn) ;
command.parameters.add("@id",sqldbtype.int ) ;
command.parameters["@id"].value = iid ;

command.parameters.add("@name",sqldbtype.nvarchar ,50) ;
command.parameters["@name"].value = strname ;

command.parameters.add("@code",sqldbtype.nvarchar ,255) ;
command.parameters["@code"].value = strcode ;

command.parameters.add("@description",sqldbtype.nvarchar ,255) ;
command.parameters["@description"].value = strdescription ;

command.parameters.add("@fatherid",sqldbtype.int ) ;
command.parameters["@fatherid"].value = ifatherid ;

command.parameters.add("@type",sqldbtype.int ) ;
command.parameters["@type"].value = itype ;

command.parameters.add("@userid",sqldbtype.int ) ;
command.parameters["@userid"].value = iuserid ;

try
{
conn.open() ;
command.executenonquery() ;
return true ;
}
catch(exception e)
{
throw(new exception("error in the database"+e.message)) ;
}
finally
{
conn.close() ;
}
}

再看一下存储过程:
public bool exesp_ddms_modify_trx(
int aiprsn_trx_no,
int aiult_incid_no,
int aiprsn_trx_status_cd,
datetime adttrx_cmpl_dt,
string astremail_addr)
{
sqlconnection conn = sqlconn.instance().connection ;

string strspname = "ddms_modify_trx" ;
sqlcommand command = new sqlcommand(strspname,conn) ;
command.commandtype = commandtype.storedprocedure ;

command.parameters.add("@prsn_trx_no",sqldbtype.smallint ) ;
command.parameters["@prsn_trx_no"].value= aiprsn_trx_no ;

command.parameters.add("@ult_incid_no",sqldbtype.int ) ;
command.parameters["@ult_incid_no"].value= aiult_incid_no ;

command.parameters.add("@prsn_trx_status_cd",sqldbtype.int ) ;
command.parameters["@prsn_trx_status_cd"].value= aiprsn_trx_status_cd ;

command.parameters.add("@trx_cmpl_dt",sqldbtype.datetime ) ;
if ( adttrx_cmpl_dt!= datetime.minvalue )
command.parameters["@trx_cmpl_dt"].value= adttrx_cmpl_dt ;
else
command.parameters["@trx_cmpl_dt"].value= dbnull.value ;

command.parameters.add("@email_addr",sqldbtype.varchar ,70) ;
if ( astremail_addr!= null )
command.parameters["@email_addr"].value= astremail_addr ;
else
command.parameters["@email_addr"].value= dbnull.value ;

try
{
conn.open() ;
command.executenonquery() ;
return true ;
}
catch(exception e)
{
throw(new exception("error in the database"+e.message)) ;
}
finally
{
conn.close() ;
}
}

上面处理表的代码也挺长的,但那只是7个字段,如果一个表含有二三十个字段的话,恐怕代码更长,同样,检查你写好的代码是否有错误也是一个痛苦的过程。
但是,写这些代码本身并没有多少难度,而且很多工作都是重复的,这就不能不启发我们通过一个程序来完成这些繁琐易出错的工作。
假如让我们来设计一个程序完成上面的代码,我们应该怎么做哪?

要完成上面的工作,我们首先应该找到我们要处理的表或存储过程。
我们也可以通过编程实现:
1.列出所有的数据库服务器
public static arraylist getserverlist()
{
arraylist alservers = new arraylist() ;
sqldmo.application sqlapp = new sqldmo.applicationclass() ;
try
{
sqldmo.namelist serverlist = sqlapp.listavailablesqlservers() ;
for(int i = 1;i<= serverlist.count;i++)
{
alservers.add(serverlist.item(i)) ;
}
}
catch(exception e)
{
throw(new exception("取数据库服务器列表出错:"+e.message)) ;
}
finally
{
sqlapp.quit() ;
}
return alservers ;
}
这样,所有的数据库服务器迷宫凝成都存在了arraylist里,我们可以用一个下拉列表来列出所有的服务器,让用户选择他需要的一个。
2。得到选定服务器所有的数据库列表
要完成这个工作,我们需要用户输入这个数据库服务器的用户名和密码,代码如下:
public static arraylist getdblist(string strservername,string strusername,string strpwd)
{
arraylist aldbs = new arraylist() ;
sqldmo.application sqlapp = new sqldmo.applicationclass() ;
sqldmo.sqlserver svr = new sqldmo.sqlserverclass() ;
try
{
svr.connect(strservername,strusername,strpwd) ;
foreach(sqldmo.database db in svr.databases)
{
if(db.name!=null)
aldbs.add(db.name) ;
}
}
catch(exception e)
{
throw(new exception("连接数据库出错:"+e.message)) ;
}
finally
{
svr.disconnect() ;
sqlapp.quit() ;
}
return aldbs ;
}
这样就得到了数据库的列表,你同样可以让用户选择一个数据库。
3。列出选定数据库所有的用户表和存储过程
在这一步我们不用sqldmo了,我们通过读取选定数据库的sysobjects表中的记录实现,当然,完成这个工作需要如下信息:你选择的数据库服务器名,用户名,密码,数据库名等信息:
public static arraylist gettablelist(string strservername,string strdbname,string strusername,string strpwd)
{
string strconstr = " data source="+ strservername +";initial catalog="+strdbname +
";password="+ strpwd+";persist security info=true;user id="+ strusername;
sqlconnection conn = new sqlconnection(strconstr) ;

arraylist altbs = new arraylist() ;

string strsql = "select * from sysobjects where (xtype='u' or xtype='p') and category<>2 order by name" ;
sqlcommand comm = new sqlcommand(strsql,conn) ;

sqldatareader sr = null ;
try
{
conn.open() ;
sr = comm.executereader() ;
while(sr.read())
{
string strname = sr["name"].tostring() ;
bool bltable = sr["xtype"].tostring().trim().toupper() == "u"?true:false ;
tableinfo tb = new tableinfo(strname,bltable) ;
altbs.add(tb) ;
}

}
catch(exception err)
{
throw(new exception("取表明列表出错:"+err.message)) ;
}
finally
{
if (sr != null)
{
sr.close() ;
sr = null ;
}
conn.close() ;
}
return altbs ;
}
这样我们就得到了所有的用户表和存储过程,注意这一句:
select * from sysobjects where (xtype='u' or xtype='p') and category<>2 order by name,这是关键的sql语句,其中xtype='u' 表示用户表,xtype='p'表示存储过程,category<>2表示不是系统存储过程。


下一步是取得选定表的所有字段或存储过程的所有参数
我们从表syscolumns取得我们想要到的东西:
strtablename = tbinfo.name ;
string strsql = "select * from syscolumns where id=( " +
" select id from sysobjects where name='"+ strtablename + "')" ;

sqldataadapter sa = new sqldataadapter(strsql,conn) ;
dataset ds = new dataset() ;
sa.fill(ds) ;
这样我们就把某个表(或存储过程)的所有字段信息放到了结果集ds里。
在syscolumns 里有一个字段xtype,该字段表示列的数据类型,俺定义了两个函数,能根据xtype的值得到对应的dotnet中的数据类型和sqldb数据类型:
private string gettype(int itype)
{
string strresult = "" ;
switch(itype)
{
case 34:
strresult = "byte[]" ;
break ;
case 35:
case 99:
case 167:
case 175:
case 231:
case 239:
strresult = "string" ;
break ;
case 48:
case 52:
case 56:
case 127:
strresult = "int" ;
break ;
case 58:
case 61:
strresult = "datetime" ;
break ;
case 59:
strresult = "single" ;
break ;
case 60:
case 106:
case 108:
case 122:
strresult = "decimal" ;
break ;
case 62:
strresult = "double" ;
break ;
case 104:
strresult = "bool" ;
break ;
default:
strresult = "none" ;//"unknow" ;
break ;
}
return strresult + " " ;
}

private string getsqldbtype(int itype)
{
string strresult = "" ;
switch(itype)
{
case 34:
strresult = "sqldbtype.image" ;
break ;
case 35:
strresult = "sqldbtype.text" ;
break ;
case 48:
strresult = "sqldbtype.tinyint" ;
break ;
case 52:
strresult = "sqldbtype.smallint" ;
break ;
case 56:
strresult = "sqldbtype.int" ;
break ;
case 58:
strresult = "sqldbtype.smalldatetime" ;
break ;
case 59:
strresult = "sqldbtype.real" ;
break ;
case 60:
strresult = "sqldbtype.money" ;
break ;
case 61:
strresult = "sqldbtype.datetime" ;
break ;
case 62:
strresult = "sqldbtype.float" ;
break ;
case 99:
strresult = "sqldbtype.ntext" ;
break ;
case 104:
strresult = "sqldbtype.bit" ;
break ;
case 106:
strresult = "sqldbtype.decimal" ;
break ;
case 108:
strresult = "sqldbtype.decimal" ;
break ;
case 122:
strresult = "sqldbtype.smallmoney" ;
break ;
case 127:
strresult = "sqldbtype.bigint" ;
break ;
case 165:
strresult = "sqldbtype.varbinary" ;
break ;
case 167:
strresult = "sqldbtype.varchar" ;
break ;
case 173:
strresult = "sqldbtype.binary" ;
break ;
case 175:
strresult = "sqldbtype.char" ;
break ;
case 189:
strresult = "sqldbtype.timestamp" ;
break ;
case 231:
strresult = "sqldbtype.nvarchar" ;
break ;
case 239:
strresult = "sqldbtype.nchar" ;
break ;
default:
strresult = "none" ;//"unknow" ;
break ;
}
return strresult + " " ;
}
当然,还有一个函数先介绍一下,后面有它的具体用法:
private string gettypeshort(int itype)
{
string strresult = "" ;
switch(gettype(itype).trim())
{
case "byte[]":
strresult = "img" ;
break ;
case "string":
strresult = "str" ;
break ;
case "int":
strresult = "i" ;
break ;
case "datetime":
strresult = "dt" ;
break ;
case "single":
strresult = "sig" ;
break ;
case "decimal":
strresult = "dec" ;
break ;
case "double":
strresult = "db" ;
break ;
case "bool":
strresult = "bl" ;
break ;
default:
strresult = "unknow" ;
break ;
}
return strresult ;
}

现在有了上面的介绍和函数基础,我们可以逐步实现我们的目标了:
1.生成数据实体类的数据成员
大家看一下,对于一个字段id,整形,我们这样定义:
private int iid ;
public int id
{
get
{
return iid ;
}
set
{
iid = value ;
}
}
其中iid表示数据类型+字段名称,其中字段名称的第一个字母大写,代码实现如下:
private string buildmember(dataset ds)
{
stringbuilder sb = new stringbuilder() ;
foreach(datarow dr in ds.tables[0].rows)
{
int itype = (byte)dr["xtype"] ;
string strtype = gettype(itype) ;
string strname = dr["name"].tostring() ;
strname = upfirstchar(strname) ;
string strshorttype = gettypeshort(itype) ;
string strprivateline = "private " + strtype + strshorttype +strname +" ;/r/n" ;
sb.append(strprivateline) ;
string strpublicline = "public " + strtype + strname +" /r/n" ;
sb.append(strpublicline) ;
sb.append("{/r/n") ;
sb.append("/tget/r/n") ;
sb.append("/t{/r/n") ;
sb.append("/t/treturn " +strshorttype +strname+ " ;/r/n") ;
sb.append("/t}/r/n") ;
sb.append("/tset/r/n") ;
sb.append("/t{/r/n") ;
sb.append("/t/t" +strshorttype +strname+ " = value ;/r/n") ;
sb.append("/t}/r/n") ;
sb.append("}/r/n") ;
sb.append("/r/n") ;
sb.append("/r/n") ;
}
return sb.tostring();
}

参数dataset ds中的ds是通过上一页的sa.fill(ds)得到的,dr["name"]表示字段名称,dr["xtype"] 是字段类型。
upfirstchar函数负责把字符串的第一个字母大写:
private string upfirstchar(string strvalue)
{
return strvalue.substring(0,1).toupper() + strvalue.substring(1,strvalue.length - 1) ;
}
这样就生成了数据实体的数据成员。
2。生成add方法
add方法生成起来有点难度,可以参考第一页的程序,我们这里是通过参数的方法来实现add的。有一点要注意,就是在对参数赋值时要考虑参数值为空的情况。代码实现如下:
private string buildaddfunction(dataset ds)
{
stringbuilder sb = new stringbuilder() ;

sb.append("public bool add()/r/n") ;
sb.append("{/r/n") ;
sb.append("/tsqlconnection conn = sqlconn.instance().connection ;/r/n") ;
sb.append("/r/n") ;
string strinsert = "/"insert into " + strtablename + "(";
string strfiledlist = "" ;
string strparamlist = "" ;
foreach(datarow dr in ds.tables[0].rows)
{
string strname = dr["name"].tostring() ;
strfiledlist = strfiledlist + strname + ", " ;
strparamlist = strparamlist + "@" + strname + ", " ;
}
strfiledlist = strfiledlist.trim().trimend(',') ;
strparamlist = strparamlist.trim().trimend(',') ;
sb.append("/tstring strsql = " + strinsert + strfiledlist+ ")/"/r/n") ;
sb.append("/t+/"values(" + strparamlist+ ")/" ;/r/n") ;
sb.append("/r/n") ;
sb.append("/tsqlcommand command = new sqlcommand(strsql,conn) ;/r/n") ;
sb.append("/r/n") ;
foreach(datarow dr in ds.tables[0].rows)
{
string strname = dr["name"].tostring() ;
string strsqltype = getsqldbtype((byte)dr["xtype"]) ;
string strshorttype = gettypeshort((byte)dr["xtype"]) ;
string strvaluename = strshorttype + upfirstchar(strname) ;
int ilen = (int16)dr["prec"] ;

string strlen = "" ;
if (strshorttype == "str")
strlen = strlen + "," + ilen ;
string strcommandname = "/tcommand.parameters.add(/"@" + strname + "/"," + strsqltype + strlen+ ") ;/r/n" ;

string strcommandvalue = "/tcommand.parameters[/"@" + strname + "/"].value = " + strvaluename + " ;/r/n" ;
string strcommandnullvalue = "/tcommand.parameters[/"@" + strname + "/"].value = dbnull.value ;/r/n" ;

sb.append(strcommandname) ;
if (strshorttype == "dt")
sb.append("/tif (" + strvaluename + "!= datetime.minvalue )/r/n") ;
else if (strshorttype == "str")
sb.append("/tif (" + strvaluename + "!= null )/r/n") ;
else if (strshorttype == "img")
sb.append("/tif (" + strvaluename + "!= null )/r/n") ;
else
sb.append("") ;
sb.append(strcommandvalue) ;
if (strshorttype == "dt" ||strshorttype == "str"||strshorttype == "img")
{
sb.append("/telse/r/n") ;
sb.append(strcommandnullvalue) ;
}
sb.append("/r/n") ;
}

sb.append(addcatchstring()) ;

sb.append("}/r/n") ;
return sb.tostring();
}


3。edit,delete的实现方法类似与add,就不详细说了,edit方法代码如下:
private string buildmodifyfunction(dataset ds)
{
stringbuilder sb = new stringbuilder() ;

sb.append("public bool modify()/r/n") ;
sb.append("{/r/n") ;
sb.append("/tsqlconnection conn = sqlconn.instance().connection ;/r/n") ;
string strmodify = "string strsql =/"update " + strtablename + " set ";

string strparamlist = "" ;
foreach(datarow dr in ds.tables[0].rows)
{
string strname = dr["name"].tostring() ;
strparamlist = strparamlist + strname + " = @" + strname + ", " ;
}
strparamlist = strparamlist.trim().trimend(',') ;
sb.append("/t" + strmodify + strparamlist + " /"/r/n") ;
sb.append("/t+ /" where id [email protected] /" ;/r/n") ;

sb.append("/tsqlcommand command = new sqlcommand(strsql,conn) ;/r/n") ;

foreach(datarow dr in ds.tables[0].rows)
{
string strname = dr["name"].tostring() ;
string strsqltype = getsqldbtype((byte)dr["xtype"]) ;
string strshorttype = gettypeshort((byte)dr["xtype"]) ;
string strvaluename = strshorttype + upfirstchar(strname) ;
int ilen = (int16)dr["prec"] ;

string strlen = "" ;
if (strshorttype == "str")
strlen = strlen + "," + ilen ;

string strcommandname = "/tcommand.parameters.add(/"@" + strname + "/"," + strsqltype + strlen + ") ;/r/n" ;
string strcommandvalue = "/tcommand.parameters[/"@" + strname + "/"].value = " + strvaluename + " ;/r/n" ;
sb.append(strcommandname) ;
sb.append(strcommandvalue) ;
sb.append("/r/n") ;
}

sb.append(addcatchstring()) ;

sb.append("}/r/n") ;
return sb.tostring();
}

4。生成存储过程的执行方法和返回结果方法。存储过程的参数也在syscolumns表里有详细的说明,里面还记录了每个参数是不是传出参数,不过在这个函数里我没有考虑参数的方向,当然,要考虑进去也不是很麻烦。
private string buildspexec(dataset ds)
{
stringbuilder sb = new stringbuilder() ;
string strfuncparam = "" ;

foreach(datarow dr in ds.tables[0].rows)
{
int itype = (byte)dr["xtype"] ;
string strname = upfirstchar(dr["name"].tostring().trimstart('@')) ;
string strtype = gettype(itype) ;
string strsqltype = getsqldbtype(itype) ;
string strshorttype = gettypeshort(itype) ;
strfuncparam = strfuncparam + "/r/n/t/t" + strtype + " a" + strshorttype + strname + ",";
}
strfuncparam = strfuncparam.trimend(',') ;
string strdef = "public bool exesp_" + strtablename + "(" + strfuncparam + ")/r/n";
sb.append(strdef) ;
sb.append("{/r/n") ;
sb.append("/tsqlconnection conn = sqlconn.instance().connection ;/r/n") ;
sb.append("/r/n") ;
sb.append("/tstring strspname = /"" + strtablename + "/" ;/r/n") ;
sb.append("/tsqlcommand command = new sqlcommand(strspname,conn) ;/r/n");
sb.append("/tcommand.commandtype = commandtype.storedprocedure ;/r/n") ;
sb.append("/r/n") ;

foreach(datarow dr in ds.tables[0].rows)
{
int itype = (byte)dr["xtype"] ;
string strparaname = dr["name"].tostring() ;
string strname = upfirstchar(strparaname.trimstart('@')) ;
string strsqltype = getsqldbtype(itype) ;
string strshorttype = gettypeshort(itype) ;
string strvalue = " a" + strshorttype + strname ;
int ilen = (int16)dr["prec"] ;

string strlen = "" ;
if (strshorttype == "str")
strlen = strlen + "," + ilen ;

string strcommandadd = "/tcommand.parameters.add(/"" + strparaname + "/"," + strsqltype + strlen + ") ;/r/n";
string strcommandvalue = "/tcommand.parameters[/"" + strparaname + "/"].value=" + strvalue + " ;/r/n";
string strcommandnull = "/tcommand.parameters[/"" + strparaname + "/"].value= dbnull.value ;/r/n";
sb.append(strcommandadd) ;
if (strshorttype == "dt")
sb.append("/tif (" + strvalue + "!= datetime.minvalue )/r/n/t") ;
else if (strshorttype == "str")
sb.append("/tif (" + strvalue + "!= null )/r/n/t") ;
else if (strshorttype == "img")
sb.append("/tif (" + strvalue + "!= null )/r/n/t") ;
else
sb.append("") ;
sb.append(strcommandvalue) ;
if (strshorttype == "dt" ||strshorttype == "str"||strshorttype == "img")
{
sb.append("/telse/r/n/t") ;
sb.append(strcommandnull) ;
}
sb.append("/r/n") ;
}
sb.append(addcatchstring()) ;

sb.append("}/r/n") ;
return sb.tostring() ;
}

对于存储过程生成结果集的函数如下:
private string buildspgetdata(dataset ds)
{
stringbuilder sb = new stringbuilder() ;
string strfuncparam = "" ;

foreach(datarow dr in ds.tables[0].rows)
{
int itype = (byte)dr["xtype"] ;
string strname = upfirstchar(dr["name"].tostring().trimstart('@')) ;
string strtype = gettype(itype) ;
string strsqltype = getsqldbtype(itype) ;
string strshorttype = gettypeshort(itype) ;
strfuncparam = strfuncparam + "/r/n/t/t" + strtype + " a" + strshorttype + strname + ",";
}
strfuncparam = strfuncparam.trimend(',') ;
string strdef = "public dataset querysp_" + strtablename + "(" + strfuncparam + ")/r/n";
sb.append(strdef) ;
sb.append("{/r/n") ;
sb.append("/tsqlconnection conn = sqlconn.instance().connection ;/r/n") ;
sb.append("/r/n") ;
sb.append("/tstring strspname = /"" + strtablename + "/" ;/r/n") ;
sb.append("/tsqlcommand command = new sqlcommand(strspname,conn) ;/r/n");
sb.append("/tcommand.commandtype = commandtype.storedprocedure ;/r/n") ;
sb.append("/r/n") ;

foreach(datarow dr in ds.tables[0].rows)
{
int itype = (byte)dr["xtype"] ;
string strparaname = dr["name"].tostring() ;
string strname = upfirstchar(strparaname.trimstart('@')) ;
string strsqltype = getsqldbtype(itype) ;
string strshorttype = gettypeshort(itype) ;
string strvalue = " a" + strshorttype + strname ;
int ilen = (int16)dr["prec"] ;

string strlen = "" ;
if (strshorttype == "str")
strlen = strlen + "," + ilen ;

string strcommandadd = "/tcommand.parameters.add(/"" + strparaname + "/"," + strsqltype + strlen + ") ;/r/n";
string strcommandvalue = "/tcommand.parameters[/"" + strparaname + "/"].value=" + strvalue + " ;/r/n";
string strcommandnull = "/tcommand.parameters[/"" + strparaname + "/"].value= dbnull.value ;/r/n";
sb.append(strcommandadd) ;
if (strshorttype == "dt")
sb.append("/tif (" + strvalue + "!= datetime.minvalue )/r/n/t") ;
else if (strshorttype == "str")
sb.append("/tif (" + strvalue + "!= null )/r/n/t") ;
else if (strshorttype == "img")
sb.append("/tif (" + strvalue + "!= null )/r/n/t") ;
else
sb.append("") ;
sb.append(strcommandvalue) ;
if (strshorttype == "dt" ||strshorttype == "str"||strshorttype == "img")
{
sb.append("/telse/r/n/t") ;
sb.append(strcommandnull) ;
}
sb.append("/r/n") ;
}

sb.append("/tsqldataadapter sdaresult = new sqldataadapter(command) ;/r/n") ;
sb.append("/tdataset ds = new dataset() ;/r/n") ;


sb.append(addcatchquerystring()) ;

sb.append("}/r/n") ;
return sb.tostring() ;

}
下面是生成结果集的执行结果:
public dataset querysp_ddms_getbank(
int aiprsn_id)
{
sqlconnection conn = sqlconn.instance().connection ;

string strspname = "ddms_getbank" ;
sqlcommand command = new sqlcommand(strspname,conn) ;
command.commandtype = commandtype.storedprocedure ;

command.parameters.add("@prsn_id",sqldbtype.int ) ;
command.parameters["@prsn_id"].value= aiprsn_id ;

sqldataadapter sdaresult = new sqldataadapter(command) ;
dataset ds = new dataset() ;
try
{
sdaresult.fill(ds) ;
}
catch(exception e)
{
throw(new exception("error in the database"+e.message)) ;
}
finally
{
sdaresult.dispose() ;
}
return ds ;
}

还有两个函数在程序中用到了,如下所示:
private string addcatchstring()
{
stringbuilder sb = new stringbuilder() ;
sb.append("/ttry/r/n") ;
sb.append("/t{/r/n") ;
sb.append("/t/tconn.open() ;/r/n") ;
sb.append("/t/tcommand.executenonquery() ;/r/n") ;
sb.append("/t/treturn true ;/r/n") ;
sb.append("/t}/r/n") ;
sb.append("/tcatch(exception e)/r/n") ;
sb.append("/t{/r/n") ;
sb.append("/t/tthrow(new exception(/"error in the database/"+e.message)) ;/r/n") ;
sb.append("/t}/r/n") ;
sb.append("/tfinally/r/n") ;
sb.append("/t{/r/n") ;
sb.append("/t/tconn.close() ;/r/n") ;
sb.append("/t}/r/n") ;
return sb.tostring() ;
}

private string addcatchquerystring()
{
stringbuilder sb = new stringbuilder() ;
sb.append("/ttry/r/n") ;
sb.append("/t{/r/n") ;
sb.append("/t/tsdaresult.fill(ds) ;/r/n") ;
sb.append("/t}/r/n") ;
sb.append("/tcatch(exception e)/r/n") ;
sb.append("/t{/r/n") ;
sb.append("/t/tthrow(new exception(/"error in the database/"+e.message)) ;/r/n") ;
sb.append("/t}/r/n") ;
sb.append("/tfinally/r/n") ;
sb.append("/t{/r/n") ;
sb.append("/t/tsdaresult.dispose() ;/r/n") ;
sb.append("/t}/r/n") ;
sb.append("/treturn ds ;/r/n") ;
return sb.tostring() ;
}

顺便说一下,文章开头的一段代码就是用这个程序生成的,当然,这种方法还有待完善的地方,希望大家指正。

最大的网站源码资源下载站,

上一篇:连接Corba和dotNet

下一篇:Dot Net的调试 - 3

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表