??????? 有时候你做关于数据库的东西的时候每做一次就要写一遍数据层的代码,这样岂不太累且与编程工作的本质不相符,那么来做一个关于针对Sql数据库及c#语言的自动完成数据库层的工具.
?????? 概述:
?????? 该工具的目标:
1.?????? 自动根据你设计好的数据库生成基本的存储过程,既 Add,update,delete,select
2. ??????自动根据你的数据库及存储过程生成C#的代码类.
3.?????? 自动把代码类添加到你的工程中去.
?????? 该工具的目标服务对象
1. 以.net为开发平台及以c#为开发语言的程序员(其它语言如 vb.net,j#等慢慢再加吧. : ) )
????? 该工具的形式
1.??最好是以.net 的外接程序形式存在,这样你打开.net时就在工具栏上岂不很方便.
?????? 该工具的价值
1.试想一下这个这样的东西,你编起关于数据库的程序来岂不很快,也很爽?
????? 该工具的开发难点(含技术及非技术)
1.?时间看有没有空了.
2. 激励够不够?(其实技术上不是太难就是有点烦我做的时候是以该工具对我及他人今后工作量的减轻会有很大帮助为动力的(即:现在烦一点以后就省事了) : ) )
3.对.net平台编程,其实主要用到EnvDet 命名空间,使用正则表达式.操作Sql数据库的系统表.
现在我把小部份(还是有点多呢,只能粘部份)代码粘在这里(其实我已经在Csdn软件版上传了,可是发布不了不知道为什么)有兴趣的跟我联系,jinlei_ren@hotmail.com qq:24936285?
_________________________________SamplesOpDBClass.txt
说明:一个文本文件用它来生成类(.cs文件)
using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;
namespace <%namespace%>
{
?public class <%class%>
?{
??private SqlConnection conn=new SqlConnection(<%connection%>);
??public <%class%>()
??{
??}
??public ArrayList <%table%>_Get(<%idtype%> ID,out string E)
??{
???SqlDataReader dr<%table%>=null;
???SqlCommand cmd<%table%>=new SqlCommand();
???cmd<%table%>.CommandType=CommandType.StoredProcedure;
???cmd<%table%>.Connection=conn;
???cmd<%table%>.CommandText="<%getproced%>";
???
???<%getparam%>
???
???ArrayList al<%table%>=new ArrayList();
???
???try
???{
????E="1";
????conn.Open();???
????dr<%table%>=cmd<%table%>.ExecuteReader();
????while(dr<%table%>.Read())
????{
?????<%arraylist%>
?????al<%table%>.Add(src<%table%>);
????}
???}
???catch(Exception e)
???{
????E=e.Message;
????al<%table%>.Clear();
????al<%table%>.Add(e.Message);
???}
???dr<%table%>.Close();
???conn.Close();
???return al<%table%>;
??}
??
??public DataSet <%table%>_Get(<%idtype%> ID,out string E,string NUL)
??{
???DataSet ds<%table%>=new DataSet();
???SqlCommand cmd<%table%>=new SqlCommand();
???cmd<%table%>.CommandType=CommandType.StoredProcedure;
???cmd<%table%>.Connection=conn;
???cmd<%table%>.CommandText="<%getproced%>";
???
???<%getparam%>
???SqlDataAdapter da<%table%>=new SqlDataAdapter(cmd<%table%>);
???
???try
???{
????E="1";
????E=da<%table%>.Fill(ds<%table%>).ToString();
???}
???catch(Exception e)
???{
????E=e.Message;
????ds<%table%>=null;
???}
???conn.Close();
???return ds<%table%>;
??}
_____________________________________________GetProcedure.txt
说明:一个文本文件,用它来生成存储过程.
CREATE PROCEDURE dbo.p_<%table%>_Get
?@<%id%> <%idtype%><%long%>
AS
BEGIN
?IF( (LEN(@<%id%>)=2) AND (CAST(@<%id%> AS CHAR(2))='-1') )--!notice if you id was -1 then update all row
?BEGIN
???? SELECT * FROM [<%table%>]
?END
?ELSE
?BEGIN
???? SELECT * FROM [<%table%>] WHERE (<%id%> = @<%id%>)
?END
END
?
_____________________cCreateClass.cs
说明:主要用来生成类的代码.
namespace AutoCreateClass
{
?///
?/// cCreateClass 的摘要说明。
?///
?internal class cCreateClass
?{
??#region 所需字符定义
??///
??/// using System;
??///
??const string _1sNameSpace="using System;";
??///
??/// using System.Xml.Serialization;
??///
??const string _2sNameSpace="using System.Xml.Serialization;";
??///
??///? namespace
??///
??const string _3sNameSpace=" namespace ";
??///
??///? [Serializable]
??///
??const string _4sNameSpace=" [Serializable] ";
................?? 字符定义用于替换 文本里的关键字.
#region 根据SqlDbType返回相应的C#Type
??///
??/// 根据SqlDbType返回相应的C#Type
??///
??///
??/// 风格是否简写
??/// 例 : string
??public string GetTypeByDBType(string Type,bool ShortTitle)
??{
???string strReturn="-1";
???string strShort="-1";
???switch(Type)
???{
????case "nvarchar": strReturn= "string";strShort="s";break;
????case "bigint": strReturn= "Int64" ;strShort="i";break;
????case "binary": strReturn= "System.Byte";strShort="bt";break;
????case "bit": strReturn= "bool";strShort="b";break;
????case "char":strReturn= "string";strShort="c";break;
????case "datetime": strReturn= "DateTime";strShort="dt";break;
????case "decimal": strReturn= "De
................Sql中的变量关键字替换成Csharp 的关键字.
?
_____________________________________dbOp.cs
说明:数据库系统表的操作类.根据它得到比如说,表名啦,存储过程参数,长度啦,之等类的东西.
using System;
using System.Data;
using System.Collections;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Xml.Serialization;
namespace AutoCreateClass
{
?///
?/// DBOpertion 的摘要说明。
?///
?public class DBOperation
?{
?? static private string sqlObjectReturnClient="select [syscolumns].name,[syscolumns].status,[syscolumns].isoutparam,[syscolumns].length,[systypes].name as type,[syscolumns].colorder from [systypes],[syscolumns],[sysobjects] where [syscolumns].id=[sysobjects].id and [systypes].xtype=[syscolumns].xtype and [systypes].name<>'sysname' and [sysobjects].name=";
??static private string sqlDB="select name from [master]..sysdatabases where name <>'user' and name<>'master' and name<>'model' and name<>'msdb' and name<>'tempdb'";
??static private string sqlProcedure="select name from ";
??static private string sqlTable="select name from sysobjects where xtype='u' and name<>'dtproperties'";
??private string m_strCurrentDB="";
??static public string connSqlInfoMessage="";
??static public string ErrorMessage="";
??public ArrayList alOutput=new ArrayList();
??private string m_strConnectionString="";
??private SqlConnection connSql=new SqlConnection();
??public DBOperation(string ConnectionStr)
??{
???connSql.ConnectionString=ConnectionStr;
??}
?
??public string strCurrentDB
??{
???get
???{
????return m_strCurrentDB;
???}
???set
???{
????m_strCurrentDB=value;
???}
??}
??public string strConnectionString
??{
???get
???{
????return m_strConnectionString;
???}
???set
???{
????m_strConnectionString=value;
???}
??}
??///
??///首先检查是否存在DBName然后检查,是否precedureName是存储过程名字。得到存储过程信息
??///检查参数是否与之相配。
??///执行过程然后返回值.
??///
??[XmlInclude(typeof(output))]
??public DataSet OPDataBaseAndGetReturnData(string procedureName,ArrayList parameter)
??{
???connSql.InfoMessage+=new SqlInfoMessageEventHandler(connSql_InfoMessage);
???ArrayList alCheckResult=CheckDBObjectName(procedureName);
???ArrayList alCheckParam=CheckProcedureParameter(procedureName);
???SqlCommand cmdSql=new SqlCommand("dbo."+procedureName,connSql);
???cmdSql.CommandType=CommandType.StoredProcedure;
???SqlDataAdapter adpSql=new SqlDataAdapter(cmdSql);
还差关于外接程序的类,正则表达式的类,不粘了,太麻烦了,而且混乱不堪,还是跟我联系吧,其实我的目的是最好希望能够有人把它扩充,比如,不仅支持.net C#?的语言,也支持vb j#?什么的,而且更有可能的话最好能够做成根据数据库中的主键能在类中生成能够反映其表逻辑关系的代码.最终形成一个能够只需设计数据库.其它都自动搞定的东西.
,jinlei_ren@hotmail.com qq:24936285? csdn:jinlei001
(任何人转载,复制及进行任何涉及本文版权的操作经需争得作者同意.?)