一、已有数据库(sql)表,快速生成对应存储过程的工具。sqlpilot.exe
sqlpilot.exe这个程序是ccf的一个朋友写的,具体请参考http://bbs.et8.net/bbs/showthread.php?t=534183
二、已有数据库表,快速生成列表、基本查询、高级查询、插入新行、删除、编辑asp.net页面的工具asp.net maker
http://www.hkvstore.com/aspnetmaker/
1.数据库操作类
以下是c#代码:
头文件:
using system;using system.web.ui;using system.globalization;using system.data;using system.data.sqlclient;namespace ns_db{public class c_db: usercontrol{
//******************************** // 返回数据库连接字符串 //********************************
public string ewconnstr() { return "password=******;persist security info=true;user id=sa;initial catalog=******;data source=
(local)"; } // end ewconnstr
//****************************************************** // 返回一个 dataview 根据 connection string & sql //******************************************************
public dataview ewdataview(string sconn, string ssql) { try {
// create a new connection object sqlconnection oconn = new sqlconnection(sconn);
// create a new dataadapter using the connection object and sql statement sqldataadapter oda = new sqldataadapter(ssql, oconn);
// create a new dataset object to fill with data dataset ods = new dataset();
// fill the dataset with data from the dataadapter object oda.fill(ods, "ewdataset"); return ods.tables[0].defaultview; } catch (sqlexception oerr) { session["dberrmsg"] = ewdataerrormessage(oerr); return null; } } // end ewdataview
//********************************* // 返回一个 dataview for linking //*********************************
public dataview ewdataviewlink(string sconn, string stable/*表名*/, string slnkfld, string sdispfld, string sdispfld2, string sfilterfld, string sorderby/*排序列*/, string sordertype/*排序规则*/, bool bdistinct/*是否剔除重复值*/, string sfilter) { string ssql; try {
// construct sql statement ssql = "select"; if (bdistinct) { ssql += " distinct"; } ssql += " [" + slnkfld + "], [" + sdispfld + "]"; if (sdispfld2 != "") { ssql += ", [" + sdispfld2 + "]"; } if (sfilterfld != "") { ssql += ", [" + sfilterfld + "]"; } ssql += " from [" + stable + "]"; if (sfilter != "") { ssql += " where " + sfilter; } if (sorderby != "") { ssql += " order by [" + sorderby + "] " + sordertype; }
// create a new connection object using the connection string sqlconnection oconn = new sqlconnection(sconn);
// create a new dataadapter using the connection object and sql statement sqldataadapter oda = new sqldataadapter(ssql, oconn);
// create a new dataset object to fill with data dataset ods = new dataset();
// fill the dataset with data from the dataadapter object oda.fill(ods, "ewdataset");
// create the textfield and valuefield columns ods.tables[0].columns.add("ewvaluefield",type.gettype("system.string"),"[" + slnkfld + "]"); if (sdispfld2 == "") { ods.tables[0].columns.add("ewtextfield",type.gettype("system.string"),"[" + sdispfld + "]"); } else { ods.tables[0].columns.add("ewtextfield",type.gettype("system.string"),"[" + sdispfld + "] +
', ' + [" + sdispfld2 + "]"); } return ods.tables[0].defaultview; } catch (sqlexception oerr) { session["dberrmsg"] = ewdataerrormessage(oerr); return null; } } // end ewdataviewlink
//********************************************************* // 根据 connection string & sql 返回 records 的数量 //*********************************************************
public int ewrecordcount(string sconn, string stable, string swhere) { string ssql; try {
// construct sql ssql = "select count(*) from [" + stable + "]"; if (swhere != "") { ssql += " where " + swhere; }
// create a new connection object using the connection string sqlconnection oconn = new sqlconnection(sconn);
// create a new dataadapter using the connection object and sql statement sqldataadapter oda = new sqldataadapter(ssql, oconn);
// create a new dataset object to fill with data dataset ods = new dataset();
// fill the dataset with data from the dataadapter object oda.fill(ods, "ewdataset"); return convert.toint32(ods.tables[0].rows[0][0]); } catch (sqlexception oerr) { session["dberrmsg"] = ewdataerrormessage(oerr); return 0; } } // end ewrecordcount
//*********************************************************** // 返回 1-page dataview 根据 connection string & sql //***********************************************************
public dataview ewdataviewpage(string sconn, string ssql, int icurrentrec, int ipagesize) { try {
// create a new connection object using the connection string sqlconnection oconn = new sqlconnection(sconn);
// create a new dataadapter using the connection object and sql statement sqldataadapter oda = new sqldataadapter(ssql, oconn);
// create a new dataset object to fill with data dataset ods = new dataset();
// fill the dataset with data from the dataadapter object oda.fill(ods, icurrentrec, ipagesize, "ewdataset"); return ods.tables[0].defaultview; } catch (sqlexception oerr) { session["dberrmsg"] = ewdataerrormessage(oerr); return null; } } // end ewdataviewpage
//************************************************* // return a datareader based on connection & sql //*************************************************
public sqldatareader ewdatareader(sqlconnection oconn, string ssql) { try {
// create a datareader object sqldatareader odr;
// create a new command object using the connection and sql statement sqlcommand ocmd = new sqlcommand(ssql, oconn);
// execute the sql statement against the command to get the datareader odr = ocmd.executereader(); return odr; } catch (sqlexception oerr) { session["dberrmsg"] = ewdataerrormessage(oerr); return null; } } // end ewdatareader
//********************************************** // return error message based on error object //**********************************************
public string ewdataerrormessage(sqlexception oerr) { string sdberrmsg; sdberrmsg = ""; for (int i = 0; i <= oerr.errors.count - 1; i++) { sdberrmsg += "message: " + oerr.errors[i].message + "" + "line number: " + oerr.errors[i].linenumber + "" + "source: " + oerr.errors[i].source + "" + "procedure: " + oerr.errors[i].procedure + ""; } return sdberrmsg; } // end ewdataerrormessage
//*************************** // return upload file name //***************************
public string ewuploadfilename(string sfilename) { string soutfilename;
// amend your logic here soutfilename = sfilename;
// return computed output file name return soutfilename; } // end ewuploadfilename
//****************************************************** // return formatted number similar to vb formatnumber // - includeleadingdigit is not supported //******************************************************
public string ewformatnumber(object ono, int idecplace, int iuseparen, int igroupdigits) { numberformatinfo onfi = (numberformatinfo) numberformatinfo.currentinfo.clone(); onfi.numberdecimaldigits = idecplace; // numdigitsafterdecimal
// includeleadingdigit: not used if (iuseparen == -1) // useparensfornegativenumbers: true { onfi.numbernegativepattern = 0; } else if (iuseparen == 0) // useparensfornegativenumbers: false { onfi.numbernegativepattern = 1; } if (igroupdigits == -1) // groupdigits: true { onfi.numbergroupseparator = ","; } else if (igroupdigits == 0) // groupdigits: false { onfi.numbergroupseparator = ""; }
// cast for different data types if (ono is short) // short { return ((short) ono).tostring("n",onfi); } else if (ono is ushort) // ushort { return ((ushort) ono).tostring("n",onfi); } else if (ono is int) // int { return ((int) ono).tostring("n",onfi); } else if (ono is uint) // uint { return ((uint) ono).tostring("n",onfi); } else if (ono is long) // long { return ((long) ono).tostring("n",onfi); } else if (ono is ulong) // ulong { return ((ulong) ono).tostring("n",onfi); } else if (ono is float) // float { return ((float) ono).tostring("n",onfi); } else if (ono is double) // double { return ((double) ono).tostring("n",onfi); } else if (ono is decimal) // decimal { return ((decimal) ono).tostring("n",onfi); } else { return ((decimal) ono).tostring("n",onfi); } }
//********************************************************** // return formatted currency similar to vb formatcurrency // - includeleadingdigit is not supported //**********************************************************
public string ewformatcurrency(object ono, int idecplace, int iuseparen, int igroupdigits) { numberformatinfo onfi = (numberformatinfo) numberformatinfo.currentinfo.clone(); onfi.currencydecimaldigits = idecplace; // numdigitsafterdecimal
// includeleadingdigit: not used if (iuseparen == -1) // useparensfornegativenumbers: true { onfi.currencynegativepattern = 0; } else if (iuseparen == 0) // useparensfornegativenumbers: false { onfi.currencynegativepattern = 1; } if (igroupdigits == -1) // groupdigits: true { onfi.currencygroupseparator = ","; } else if (igroupdigits == 0) // groupdigits: false { onfi.currencygroupseparator = ""; }
// cast for different data types if (ono is short) // short { return ((short) ono).tostring("c",onfi); } else if (ono is ushort) // ushort { return ((ushort) ono).tostring("c",onfi); } else if (ono is int) // int { return ((int) ono).tostring("c",onfi); } else if (ono is uint) // uint { return ((uint) ono).tostring("c",onfi); } else if (ono is long) // long { return ((long) ono).tostring("c",onfi); } else if (ono is ulong) // ulong { return ((ulong) ono).tostring("c",onfi); } else if (ono is float) // float { return ((float) ono).tostring("c",onfi); } else if (ono is double) // double { return ((double) ono).tostring("c",onfi); } else if (ono is decimal) // decimal { return ((decimal) ono).tostring("c",onfi); } else { return ((decimal) ono).tostring("c",onfi); } }
//******************************************************** // return formatted percent similar to vb formatpercent // - includeleadingdigit is not supported //********************************************************
public string ewformatpercent(object ono, int idecplace, int iuseparen, int igroupdigits) { numberformatinfo onfi = (numberformatinfo) numberformatinfo.currentinfo.clone(); onfi.currencydecimaldigits = idecplace; // numdigitsafterdecimal
// includeleadingdigit: not used if (iuseparen == -1) // useparensfornegativenumbers: true { onfi.currencynegativepattern = 0; } else if (iuseparen == 0) // useparensfornegativenumbers: false { onfi.currencynegativepattern = 1; } if (igroupdigits == -1) // groupdigits: true { onfi.currencygroupseparator = ","; } else if (igroupdigits == 0) // groupdigits: false { onfi.currencygroupseparator = ""; }
// cast for different data types if (ono is short) // short { return ((short) ono).tostring("p",onfi); } else if (ono is ushort) // ushort { return ((ushort) ono).tostring("p",onfi); } else if (ono is int) // int { return ((int) ono).tostring("p",onfi); } else if (ono is uint) // uint { return ((uint) ono).tostring("p",onfi); } else if (ono is long) // long { return ((long) ono).tostring("p",onfi); } else if (ono is ulong) // ulong { return ((ulong) ono).tostring("p",onfi); } else if (ono is float) // float { return ((float) ono).tostring("p",onfi); } else if (ono is double) // double { return ((double) ono).tostring("p",onfi); } else if (ono is decimal) // decimal { return ((decimal) ono).tostring("p",onfi); } else { return ((decimal) ono).tostring("p",onfi); } }} // end class} // end namespace