我做的一个SQL -> C# AutoCode

博主做项目时因重复列表表格输入工作,写了AutoCode生成代码。看到Wintle同学的CodePlua后想起此事,讨论在数据库操作中这种做法是否实用。博主将SQL编程为C# Class,屏蔽SQL,以行为单位操作数据库表很方便,若有用处会完善发布。

    有一次做项目的时候,遇到了一大堆的列表表格要输入,开始做了几个表,结果都是一样的代码,甚是郁闷,索性写了个AutoCode来生成,后来项目做完了,没有怎么碰到这样郁闷的工作,也就把它忘了。今天看到了Wintle同学做了个CodePlua(CodePlus C# 代码生成器 V1.0 推出啦!) ,我才想起我也有个这样的冬冬。
AutoCodeAbout.gif

    我不是要竞争哈,我是觉得可以讨论一下这种做法在数据库操作中算不算郁闷,我把SQL编程C# Class,按字段对应,然后再程序中完全SQL屏蔽掉了。这种方法不算广义的OR Mapping,因为根本没有严格意义上的Relation嘛。不过这样操作数据库表非常的方便,特别是以行为单位操作的时候,SQL的字段完全可以在程序中IntelliSence,编程比较爽teeth_smile.gif哈。

    示例代码(SQL):

None.gif create   table   [ TableDemo ] (
None.gifID 
int   identity ( 1 , 1 ),
None.gifFieldA 
nvarchar ( 50 ),
None.gifFieldB 
ntext
None.gifFieldC 
datetime ,
None.gifFieldD 
decimal )

    C# Class:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// Automatically Code for Table 'TableDemo',
InBlock.gif
/// by Birdshome AutoCode Engine V.1.0
InBlock.gif
/// Copyright (C) 2004.1 Birdshome, HIT
ExpandedBlockEnd.gif
/// </summary>

ContractedBlock.gif ExpandedBlockStart.gif TableDemo Class #region TableDemo Class
InBlock.gif
public class TableDemo
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
private System.Int32 m_ID;
InBlock.gif    
private System.String m_FieldA;
InBlock.gif    
private System.String m_FieldB;
InBlock.gif    
private System.DateTime m_FieldC;
InBlock.gif    
private System.Decimal m_FieldD;
InBlock.gif    
private System.Data.SqlClient.SqlCommand SqlCmd;
InBlock.gif
InBlock.gif    
private bool bSetFieldA    = false;
InBlock.gif    
private bool bSetFieldB    = false;
InBlock.gif    
private bool bSetFieldC    = false;
InBlock.gif    
private bool bSetFieldD    = false;
InBlock.gif
InBlock.gif    
private static string _TableName = "TableDemo";
InBlock.gif
InBlock.gif    
public System.Int32 ID
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return m_ID;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public System.String FieldA
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return m_FieldA;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( value != null && m_FieldA != value )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_FieldA 
= value;
InBlock.gif                bSetFieldA 
= true;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public System.String FieldB
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return m_FieldB;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( value != null && m_FieldB != value )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_FieldB 
= value;
InBlock.gif                bSetFieldB 
= true;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public System.DateTime FieldC
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return m_FieldC;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_FieldC 
= value;
InBlock.gif            bSetFieldC 
= true;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public System.Decimal FieldD
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return m_FieldD;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            m_FieldD 
= value;
InBlock.gif            bSetFieldD 
= true;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public TableDemo()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SqlCmd 
= new System.Data.SqlClient.SqlCommand();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public TableDemo(int iID) : this()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        m_ID 
= iID;
InBlock.gif        Load();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected bool Load()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return Load(m_ID);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
protected bool Load(int iID)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SqlCmd.Connection 
= GetSqlConnection();
InBlock.gif        SqlCmd.CommandText 
= "Select * From [" + _TableName + "] Where ([ID] = @ID)";
InBlock.gif        SqlCmd.Parameters.Add(
"@ID", System.Data.SqlDbType.Int);
InBlock.gif        SqlCmd.Parameters[
"@ID"].Value = iID;
InBlock.gif        System.Data.SqlClient.SqlDataReader drFields;
InBlock.gif        drFields 
= SqlCmd.ExecuteReader();
InBlock.gif        
bool bLoadSuccess = false;
InBlock.gif        
if ( drFields.Read() )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object objFieldA = drFields["FieldA"];
InBlock.gif            
if ( !( objFieldA is System.DBNull ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_FieldA 
= (System.String)drFields["FieldA"];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
object objFieldB = drFields["FieldB"];
InBlock.gif            
if ( !( objFieldB is System.DBNull ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_FieldB 
= (System.String)drFields["FieldB"];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
object objFieldC = drFields["FieldC"];
InBlock.gif            
if ( !( objFieldC is System.DBNull ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_FieldC 
= (System.DateTime)drFields["FieldC"];
ExpandedSubBlockEnd.gif            }

InBlock.gif            
object objFieldD = drFields["FieldD"];
InBlock.gif            
if ( !( objFieldD is System.DBNull ) )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                m_FieldD 
= (System.Decimal)drFields["FieldD"];
ExpandedSubBlockEnd.gif            }

InBlock.gif            bLoadSuccess 
= true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
bool bNonUnique = drFields.Read();
InBlock.gif        drFields.Close();
InBlock.gif        drFields 
= null;
InBlock.gif        SqlCmd.Connection.Close();
InBlock.gif        SqlCmd.Parameters.Clear();
InBlock.gif        
if ( bNonUnique || !bLoadSuccess )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string strMessage = "The identity isn't unique.";
InBlock.gif            
throw new System.Exception(strMessage);
ExpandedSubBlockEnd.gif        }

InBlock.gif        m_ID 
= iID;
InBlock.gif        
return bLoadSuccess;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Save datas
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public void Save()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Save(m_ID);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Save the datas of row which ID equal iIdentity.
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <param name="iIdentity"></param>

InBlock.gif    private void Save(int iIdentity)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( iIdentity <= 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SaveAs();
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

InBlock.gif        System.Text.StringBuilder strbSql;
InBlock.gif        strbSql 
= new System.Text.StringBuilder();
InBlock.gif        strbSql.Append(
"Update [" + _TableName + "] Set");
InBlock.gif        System.Data.SqlClient.SqlParameterCollection spc;
InBlock.gif        spc 
= SqlCmd.Parameters;
InBlock.gif        
if ( bSetFieldA )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strbSql.Append(
", [FieldA] = @FieldA");
InBlock.gif            spc.Add(
"@FieldA", System.Data.SqlDbType.NVarChar, 50);
InBlock.gif            spc[
"@FieldA"].Value = m_FieldA;
InBlock.gif            bSetFieldA 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ( bSetFieldB )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strbSql.Append(
", [FieldB] = @FieldB");
InBlock.gif            spc.Add(
"@FieldB", System.Data.SqlDbType.NText);
InBlock.gif            spc[
"@FieldB"].Value = m_FieldB;
InBlock.gif            bSetFieldB 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ( bSetFieldC )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strbSql.Append(
", [FieldC] = @FieldC");
InBlock.gif            spc.Add(
"@FieldC", System.Data.SqlDbType.DateTime);
InBlock.gif            spc[
"@FieldC"].Value = m_FieldC;
InBlock.gif            bSetFieldC 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ( bSetFieldD )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strbSql.Append(
", [FieldD] = @FieldD");
InBlock.gif            spc.Add(
"@FieldD", System.Data.SqlDbType.Decimal);
InBlock.gif            spc[
"@FieldD"].Value = m_FieldD;
InBlock.gif            bSetFieldD 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if( spc.Count > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            spc.Add(
"@ID", System.Data.SqlDbType.Int);
InBlock.gif            spc[
"@ID"].Value = m_ID;
InBlock.gif            strbSql.Append(
" Where ([ID] = @ID)");
InBlock.gif            strbSql.Replace(
" Set,"" Set ");
InBlock.gif            SqlCmd.CommandText 
= strbSql.ToString();
InBlock.gif            SqlCmd.Connection 
= GetSqlConnection();
InBlock.gif            SqlCmd.ExecuteNonQuery();
InBlock.gif            SqlCmd.Connection.Close();
InBlock.gif            SqlCmd.Parameters.Clear();
InBlock.gif            m_ID 
= iIdentity;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Save the datas of current row to the new data row.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public void SaveAs()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Data.SqlClient.SqlParameterCollection spc;
InBlock.gif        spc 
= SqlCmd.Parameters;
InBlock.gif        System.Text.StringBuilder strbValues;
InBlock.gif        strbValues 
= new System.Text.StringBuilder();
InBlock.gif
InBlock.gif        
if ( bSetFieldA )
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strbValues.Append(
", @FieldA");
InBlock.gif            spc.Add(
"@FieldA", System.Data.SqlDbType.NVarChar, 50);
InBlock.gif            spc[
"@FieldA"].Value = m_FieldA;
InBlock.gif            bSetFieldA 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ( bSetFieldB )
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strbValues.Append(
", @FieldB");
InBlock.gif            spc.Add(
"@FieldB", System.Data.SqlDbType.NText);
InBlock.gif            spc[
"@FieldB"].Value = m_FieldB;
InBlock.gif            bSetFieldB 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ( bSetFieldC )
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strbValues.Append(
", @FieldC");
InBlock.gif            spc.Add(
"@FieldC", System.Data.SqlDbType.DateTime);
InBlock.gif            spc[
"@FieldC"].Value = m_FieldC;
InBlock.gif            bSetFieldC 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if ( bSetFieldD )
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strbValues.Append(
", @FieldD");
InBlock.gif            spc.Add(
"@FieldD", System.Data.SqlDbType.Decimal);
InBlock.gif            spc[
"@FieldD"].Value = m_FieldD;
InBlock.gif            bSetFieldD 
= false;
ExpandedSubBlockEnd.gif        }
        string strFields, strValues;
InBlock.gif        
if ( strbValues.Length > 3 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SqlCmd.Connection 
= GetSqlConnection();
InBlock.gif            strValues 
= strbValues.ToString().Substring(2);
InBlock.gif            strFields 
= strbValues.Replace(", @""], [").ToString();
InBlock.gif            strFields 
= strFields.Substring(3);
InBlock.gif            SqlCmd.CommandText 
= "Insert Into [" + _TableName + "] (" + strFields
InBlock.gif                
+ "]) Values(" + strValues + ")";
InBlock.gif            
lock(this)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlCmd.ExecuteNonQuery();
InBlock.gif                SqlCmd.CommandText 
= "Select @@IDENTITY";
InBlock.gif                m_ID 
= System.Convert.ToInt32(SqlCmd.ExecuteScalar());
ExpandedSubBlockEnd.gif            }

InBlock.gif            SqlCmd.Connection.Close();
InBlock.gif            SqlCmd.Parameters.Clear();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Copy the row to destination row which ID equal iDesID.
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <param name="iDesID"></param>

InBlock.gif    public void CopyTo(int iDesID)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        bSetFieldA    
= true;
InBlock.gif        bSetFieldB    
= true;
InBlock.gif        bSetFieldC    
= true;
InBlock.gif        bSetFieldD    
= true;
InBlock.gif
InBlock.gif        Save(iDesID);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
~TableDemo()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SqlCmd.Dispose();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Create and return the database connection
InBlock.gif    
/// </summary>
ExpandedSubBlockEnd.gif    
/// <returns>the opened database connection</returns>

InBlock.gif    protected static System.Data.SqlClient.SqlConnection GetSqlConnection()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string strConn = "SqlConnectionString";
InBlock.gif        strConn 
= System.Configuration.ConfigurationSettings.AppSettings[strConn];
InBlock.gif        System.Data.SqlClient.SqlConnection SqlConn;
InBlock.gif        SqlConn 
= new System.Data.SqlClient.SqlConnection(strConn);
InBlock.gif        SqlConn.Open();
InBlock.gif        
return SqlConn;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
private static int GetValue(string strSql)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Data.SqlClient.SqlCommand SqlCmd;
InBlock.gif        SqlCmd 
= new System.Data.SqlClient.SqlCommand();
InBlock.gif        SqlCmd.Connection 
= GetSqlConnection();
InBlock.gif        SqlCmd.CommandText 
= strSql;
InBlock.gif        
object obj = SqlCmd.ExecuteScalar();
InBlock.gif        SqlCmd.Connection.Close();
InBlock.gif        
if ( obj is System.DBNull ) return -1;
InBlock.gif        
return (int)obj;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static int GetMaxID()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string strSql = "Select Max(ID) From [{0}]";
InBlock.gif        
return GetValue(String.Format(strSql, _TableName));
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static bool IsValueExist(string strUnique, string strValue, int iExceptID)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.Data.SqlClient.SqlCommand SqlCmd;
InBlock.gif        SqlCmd 
= new System.Data.SqlClient.SqlCommand();
InBlock.gif        SqlCmd.Connection 
= GetSqlConnection();
InBlock.gif        
string strSql = "Select Count(*) From [{0}] Where ([{1}] = @Value)";
InBlock.gif        strSql 
= String.Format(strSql, _TableName, strUnique);
InBlock.gif        SqlCmd.Parameters.Add(
"@Value", System.Data.SqlDbType.NVarChar).Value = strValue;
InBlock.gif        
if ( iExceptID >= 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strSql 
= String.Format("{0} AND ([ID] <> '{1}')", strSql, iExceptID);
ExpandedSubBlockEnd.gif        }

InBlock.gif        SqlCmd.CommandText 
= strSql;
InBlock.gif        
int iCount = (int)SqlCmd.ExecuteScalar();
InBlock.gif        SqlCmd.Connection.Close();
InBlock.gif        
return !(iCount == 0);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static bool IsValueExist(string strUnique, string strValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return IsValueExist(strUnique, strValue, -1);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
#endregion

    我比较懒,没有用View,直接就构建SQL操作表。
   
    如果这种东西有用处,我就在好好收拾它一下,再release。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值