首页/技术开发/内容

dotnet代码自动生成机的完成

技术开发2023-12-19 阅读()
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 =@id \" ;\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" (北联网教程,专业提供视频软件下载)

第1页  第2页  第3页  第4页  第5页  第6页  第7页  第8页  第9页  第10页  第11页  第12页  第13页 

……

相关阅读