Click here to Skip to main content
Licence 
First Posted 13 Jul 2006
Views 19,219
Downloads 288
Bookmarked 12 times

Auto create data layout c# code and sql procedure for .net 2005

By | 13 Jul 2006 | Article
auto create .net code

Sample Image - DodeAuto1.jpg

amende

my english very poor.

instruction

This is a .net2005 Addins ,Use to auto create c# source code and

sql procedure by database table logic and templet files for operated database.

theory

first: get the database information by sql system table or sql sp_help

command, the information include table name,table column,table column type......

 public DataSet GetTableInfo(string tableName)
        {
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter("dbo.sp_help [" + 

tableName + "]", connSql);

            try
            {
                connSql.Open();
                adp.Fill(ds);
                DataSet dsTmp = new DataSet();
                dsTmp = GetTableInfo(dsTmp.Tables[0].Rows[0]

["Name"].ToString(), "name");
                ds.Tables[0].Columns.Add("Id");
                ds.Tables[0].Rows[0]["Id"] = dsTmp.Tables[0].Rows[0]["Id"];

            }
            catch (Exception e)
            {
                string i = e.Message;
            }
            finally
            {
                connSql.Close();
            }
            return ds;
        }

second: create table logic by database information , and save information to a xml file.

thirdly: create templet files

___________________________________________sql procedure templet files

<add% @-outputfield-="@@IDENTITY" />
<add%
CREATE PROCEDURE dbo.p_<%tabname%>_Add
 <%paddparm%>
AS
BEGIN 
 INSERT INTO [<%tabname%>]
 <%addintfield%>
 VALUES
 <%addvaluefield%>
 
 <iden- SET @-outputfield- = @@IDENTITY -iden>
END
<%GO%>
%add>
... ...

___________________________________________source code templet files

    public class <%class%>OP
    {
        private SqlConnection conn=new 

SqlConnection(@"<%connstr%>");

        public <%class%>OP()
        {
        }

        public ArrayList <%tabname%>_Get(<parkey- />out string E)
        {
            SqlDataReader dr<%tabname%>=null;
            SqlCommand cmd<%tabname%>=new SqlCommand();
            

cmd<%tabname%>.CommandType=CommandType.StoredProcedure;
            cmd<%tabname%>.Connection=conn;
            cmd<%tabname%>.CommandText="p_<%tabname%>_Get";
            
            
<KEYPAR-

            


cmd#tabname#.Parameters.Add("@#field#",#fieldtype#,#fieldlength#);

            
cmd#tabname#.Parameters["@#field#"].Value=#field#;

            
-keypar> 
            
            ArrayList al<%tabname%>=new ArrayList();
            
            try
            {
                E="1";
                conn.Open();            
                

dr<%tabname%>=cmd<%tabname%>.ExecuteReader();
                while(dr<%tabname%>.Read())
                {
                    <%tabname%> src<%class%>=new 

<%class%>();
                    
                

    <GETSRC# #getsrc src#tabname#.#field#='#convertstr#(dr#tabname#["#field#"]);'>
                    
        

            al<%tabname%>.Add(src<%tabname%>);
                }
            }
            catch(Exception e)
            {
                E=e.Message;
                al<%tabname%>=null;
            }
            dr<%tabname%>.Close();
            conn.Close();
            return al<%tabname%>;
        }
... ...

fourthly: create source code file by database information and table

logic xml files, create sql procedure by database information.

(use database information replace templet files some code,

for example:<%tabname%> is sql table name)

defect

the templet files is not enough in reason .

hope

hope that net firend will incessant upgrade

create code examples

____________________________________________________ .CS
using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;

namespace DodeAutoTest
{
 public class OrderDetailsOP
 {
  private SqlConnection conn=new 

SqlConnection(@"server=info-jinlei\netsdk;database=northwind;user 

id=sa;pwd=sa");

  public OrderDetailsOP()
  {
  }

  public ArrayList OrderDetails_Get(out string E)
  {
   SqlDataReader drOrderDetails=null;
   SqlCommand cmdOrderDetails=new SqlCommand();
   cmdOrderDetails.CommandType=CommandType.StoredProcedure;
   cmdOrderDetails.Connection=conn;
   cmdOrderDetails.CommandText="p_OrderDetails_Get";
   
   
   
   ArrayList alOrderDetails=new ArrayList();
   
   try
   {
    E="1";
    conn.Open();   
    drOrderDetails=cmdOrderDetails.ExecuteReader();
    while(drOrderDetails.Read())
    {
     OrderDetails srcOrderDetails=new OrderDetails();
     
     
     srcOrderDetails.OrderID=Convert.ToInt32(drOrderDetails["OrderID"]);

... ...

_____________________________________________________________ sql procedure

ALTER  PROCEDURE dbo.p_OrderDetails_UpdByOrderID
 @OrderID int,
 @ProductID int,
@UnitPrice money,
@Quantity smallint,
@Discount real
AS
BEGIN
 UPDATE
 [OrderDetails]
 SET
 

ProductID=@ProductID,UnitPrice=@UnitPrice,Quantity=@Quantity,Discount=@Disco

unt
 WHERE
 OrderID = @OrderID
END

GO

... ...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

jinlei



China China

Member

风,吹着银白鹅毛大雪漫天飞舞,此时却艳阳高照。
你,透过纷飞的雪片却能看到道道阳光被不时斩断。

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSome comments PinadminChris Maunder3:30 14 Jul '06  
GeneralRe: Some comments Pinmemberjinlei14:53 15 Jul '06  
GeneralGenerating PinmemberArkonXX1:59 14 Jul '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 14 Jul 2006
Article Copyright 2006 by jinlei
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid