该文章中的数据访问层中有一个属性一个字段以及一个方法。属性是一个获得连接对象的属性,字段是一个连接对象的私有成员,方法是一个Get方法,得到一个查询结果集。源代码生成以后就象下面这个样子:
using System;
using System.Data;
using System.Data.SqlClient;
namespace Power.Cuike519 {
public class PowerDataAccess {
private SqlConnection m_connection;
public PowerDataAccess() {
}
public virtual SqlConnection Connection {
get {
return this.m_connection;
}
set {
this.m_connection = value;
}
}
public virtual DataSet GetAllAuthors(string s_State) {
try {
if (this.m_connection!=null) {
System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(string.Format("select * from authors where state like '{0}'",s_State),this.m_connection);
System.Data.DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
else {
return null;
}
}
catch (System.ApplicationException ex) {
throw ex;
}
finally {
this.m_connection.Close();
}
}
}
}
using System.IO;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.CodeDom;
using System.Reflection;
srouce: 如何使用.NET生成C#源代码
在找MyGeneration时发现一个网站:http://www.codegeneration.net/
上面收集了一些和代码生成有关的资料。特别是代码生成的工具,看得人眼花缭乱。
伤心的是C#的代码生成工具开源和免费的不太多