Do a .NET external connection for automatic completion of the data layer, only support C # language

xiaoxiao2021-03-06  157

??????? Sometimes you have to write a code from the data layer when you do something about the database, this is not too tired and does not match the essence of programming work, then do a for SQL Automatically complete the database layer of the database and C # language.

?????? Overview:

?????? The target of the tool:

1. ?????? Automatically generate basic stored procedures based on your design, Idd, Update, Delete, SELECT

2. ?????? Automatically generate C # code classes based on your database and stored procedure.

3. ?????? Automatically add the code class to your project.

???????? Target service object

1. Slowly add the programmer for the development platform and the program for the development of the language (other languages ​​such as VB.NET, J #, etc.:))

?????

1. It is best to exist in the form of .NET's external procedure so that you open .NET is not very convenient to the toolbar.

?????? the value of the tool

1. Imagine this thing, your program about the database is not very fast, it is also very cool?

???????????????????????????????????????????????????????????????????????????????????

1. Is there any time?

2. Inspiring enough? (In fact, it is a little annoying me to do it. When I do it, I have a great help for me to reduce the workload of others and others (ie: I am annoying now. ) :))

3. Programming the .NET platform, in fact, it is mainly used to envdet namespaces, using regular expressions. Operate the system table of the SQL database.

Now I put a small part (still a little more, only sticky some) code stick this (in fact I have uploaded in the CSDN software version, but I can't release it, I don't know why) Interested, JINLEI_REN @ Hotmail .com qq: 24936285?

_________________________________ samplesopdbclass.txt

Description: A text file uses it to generate classes (.cs file)

Using system.data; 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%>. ComMmanDText = "<% 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%> ??? SqlDataada Pter 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

Description: A text file that uses it to generate a stored procedure.

Create Procedure DBO.P _ <% Table%> _ get? @ <% ID%> <% idType%> <% long%> asbegin? IF ((@ <% ID%>) = 2) and (casT (@ <% ID%> As char (2)) = '- 1')) -! NOTICE IF you id? begin ???? SELECT * FROM [<% Table%>]? END ? Else? Begin ???? Select * from [<% Table%>] where (<% id%> = @ <% ID%>)? Endend?

_____________________ ccreateclass.cs

Description: The code mainly used to generate classes.

Namespace autocreateclass {? ///

/// CCREATECLASS summary description. ? ///

? INTERNAL CLASS CCREATECLASS

? {

? # Region's character definition

?? ///

?? // 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]";

.............. ?? Characters define the keywords used to replace the text.

#Region Returns the corresponding C # type ?? ////

?? // Return the corresponding C # type ?? //// /// according to SqldbType

?? ///

?? ///

Style is abbreviated

?? ///

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"; strret = "bt"; break; ???? case "bit": strreturn = "bool"; strrethort = " b "; Break; ???? case" char ": strreturn =" string "; string"; strshort = "c"; break; ???? case "datetime": strreturn = "datetime"; strreth = "dt"; Break ; ???? case "decimal": strreturn = "de ................ The variable key in SQL replaces the keywords in CSHARP.

?

_____________________________________ DBOP.CS

Description: The operation class of the database system table. Based on it, for example, the table name, stored procedure parameters, length, etc.

Using system.data; using system.collections; using system.data.sqltypes; using system.data.sqlclient; use system.xml.serialization; namespace autocreateclass {? ///

"/// DBOPERTION summary description. ? ///

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'"; ??prate string m_strcurrentdb = "

?? static public string connSqlinfubensage = ""; ?? static public string errorMessage = ""

?? public arraylist aloutput = new arraylist ();

??prate 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; ???} ??} ?? ///

?? /// First check if there is DBNAME and check, whether precedurename is the stored procedure name. Get stored procedure information ?? // check whether the parameter is matched. ?? // / The execution process then returns the value. ?? ///

?? [XMLinClude (Typeof (Output))]

?? Public DataSet OpdataBaseandGetReturndata (String ProcedureName, ArrayList Parameter)

?? {

??? connsql.infomessage = new sqlinfomessageeventhandler (connSQL_INFMESSAGE);

??? arraylist alCheckResult = CheckDbObjectName (ProcedureName);

??? ArrayList Alcheckparam = CheckProcedureParameter (ProcedureName);

??? SqlCommand cmdSql = new SqlCommand ( "dbo." ProcedureName, connSql); ??? cmdSql.CommandType = CommandType.StoredProcedure; ??? SqlDataAdapter adpSql = new SqlDataAdapter (cmdSql);

Also bad about the class of the external program, the class of regular expressions, not sticking, too much trouble, and confusion, or contact me, in fact, my purpose is to enhance someone to expand it, for example, not only support The language of the .NET C #?, also supports VB J #? What, and more likely, it is best to make a code that can generate a logical relationship in the class according to the primary key in the database. Finally forming a only need Design the database. Other things are automatically done.

, JINLEI_REN @ Hotmail.com QQ: 24936285? 9cbs: JINLEI001

(Any person reprint, copying and making any operation involving copyright of this article needs to be consent.?)

转载请注明原文地址:https://www.9cbs.com/read-128062.html

New Post(0)