1,数据库的表结构
除了CLASSNAME,和PARENTPATH外,全部为数字类型,SQL SERVER里的INT ,而且,没有默认值
这里对字段做个说明:
2,部署:只需要把catalog.dll复制到项目的bin下,而且适当修改下web.config就可以了,同时支持ACCESS和SQL SERVER数据库,web.config配置如下:
<appSettings>
<add key="cnstr" value="provider=microsoft.jet.oledb.4.0;data source=d:/omeweb2/catalog/db/obj.mdb" />
<!--<add key="cnstr" value="server=(local);database=test;uid=sa;pwd=;" />-->
<!--<add key="dbtype" value="sqlserver" />-->
</appSettings>
<add key="cnstr" value="provider=microsoft.jet.oledb.4.0;data source=d:/omeweb2/catalog/db/obj.mdb" />
<!--<add key="cnstr" value="server=(local);database=test;uid=sa;pwd=;" />-->
<!--<add key="dbtype" value="sqlserver" />-->
</appSettings>
这里我把用于SQL SERVER的给注释掉了,您可以根据实际情况来修改.
3,本示例核心为idb.cs,db.cs和action.cs,分别说明下作用
idb.cs:数据库操作类的接口,代码如下:
using System;
using System.Data;
namespace catalog
{
/// <summary>
/// idb 的摘要说明。
/// </summary>
interface idb
{
//
//void open();构造函数当然不能在接口里声明
System.Data.IDbConnection getcon
{
get;
//set;
}
string constr
{
get;
}
System.Data.IDbCommand command(string sql);
int exesql(string sql);
object getvalue(string sql);
void close();
DataTable getdata(string sql);
System.Data.IDataReader getdr(string sql);
}
}
using System.Data;
namespace catalog
{
/// <summary>
/// idb 的摘要说明。
/// </summary>
interface idb
{
//
//void open();构造函数当然不能在接口里声明
System.Data.IDbConnection getcon
{
get;
//set;
}
string constr
{
get;
}
System.Data.IDbCommand command(string sql);
int exesql(string sql);
object getvalue(string sql);
void close();
DataTable getdata(string sql);
System.Data.IDataReader getdr(string sql);
}
}
db.cs实例这个接口:
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
//using System.Web;
namespace catalog
{
/// <summary>
/// db 的摘要说明。
/// </summary>
public class db:idb
{
private IDbConnection con;
private IDbCommand cm;
private string dbtype="access";
public db()
{
//
// TODO: 在此处添加构造函数逻辑
//
dbtype=ConfigurationSettings.AppSettings["dbtype"];
if (dbtype==null)
dbtype="";
if (dbtype.ToLower()=="sqlserver")
{
con=new SqlConnection();
cm= new SqlCommand();
}
else
{
con=new OleDbConnection();
cm= new OleDbCommand();
}
string cnstring=ConfigurationSettings.AppSettings["cnstr"];
con.ConnectionString=cnstring;
open();
cm.Connection=con;
}
public db(string constr)
{
//
// TODO: 在此处添加构造函数逻辑
//
dbtype=ConfigurationSettings.AppSettings["dbtype"];
if (dbtype==null)
dbtype="";
if (dbtype.ToLower()=="sqlserver")
{
con=new SqlConnection();
cm= new SqlCommand();
}
else
{
con=new OleDbConnection();
cm= new OleDbCommand();
}
con.ConnectionString=constr;
open();
cm.Connection=con;
}
private void open()
{
con.Open();
}
public System.Data.IDbConnection getcon
{
get{return con;}
//set{};
}
public int exesql(string sql)
{
cm.CommandText=sql;
return cm.ExecuteNonQuery();
}
public object getvalue(string sql)
{
cm.CommandText=sql;
//return cm.ExecuteScalar();
object o=cm.ExecuteScalar();
return o;
}
public void close()
{
cm.Dispose();
con.Close();
con.Dispose();
con=null;
}
public DataTable getdata(string sql)
{
DataTable dt=new DataTable();
if (dbtype.ToLower()=="sqlserver")
{
SqlDataAdapter adapter = new SqlDataAdapter();
cm.CommandText=sql;
adapter.SelectCommand=(SqlCommand)cm;
adapter.Fill(dt);
}
else
{
OleDbDataAdapter adapter = new OleDbDataAdapter();
cm.CommandText=sql;
adapter.SelectCommand=(OleDbCommand)cm;
adapter.Fill(dt);
}
return dt;
}
public IDataReader getdr(string sql)
{
cm.CommandText=sql;
return cm.ExecuteReader();
}
public string constr
{
get{return ConfigurationSettings.AppSettings["cnstr"];}
}
public System.Data.IDbCommand command(string sql)
{
cm.CommandText=sql;
return cm;
}
}
}
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
//using System.Web;
namespace catalog
{
/// <summary>
/// db 的摘要说明。
/// </summary>
public class db:idb
{
private IDbConnection con;
private IDbCommand cm;
private string dbtype="access";
public db()
{
//
// TODO: 在此处添加构造函数逻辑
//
dbtype=ConfigurationSettings.AppSettings["dbtype"];
if (dbtype==null)
dbtype="";
if (dbtype.ToLower()=="sqlserver")
{
con=new SqlConnection();
cm= new SqlCommand();
}
else
{
con=new OleDbConnection();
cm= new OleDbCommand();
}
string cnstring=ConfigurationSettings.AppSettings["cnstr"];
con.ConnectionString=cnstring;
open();
cm.Connection=con;
}
public db(string constr)
{
//
// TODO: 在此处添加构造函数逻辑
//
dbtype=ConfigurationSettings.AppSettings["dbtype"];
if (dbtype==null)
dbtype="";
if (dbtype.ToLower()=="sqlserver")
{
con=new SqlConnection();
cm= new SqlCommand();
}
else
{
con=new OleDbConnection();
cm= new OleDbCommand();
}
con.ConnectionString=constr;
open();
cm.Connection=con;
}
private void open()
{
con.Open();
}
public System.Data.IDbConnection getcon
{
get{return con;}
//set{};
}
public int exesql(string sql)
{
cm.CommandText=sql;
return cm.ExecuteNonQuery();
}
public object getvalue(string sql)
{
cm.CommandText=sql;
//return cm.ExecuteScalar();
object o=cm.ExecuteScalar();
return o;
}
public void close()
{
cm.Dispose();
con.Close();
con.Dispose();
con=null;
}
public DataTable getdata(string sql)
{
DataTable dt=new DataTable();
if (dbtype.ToLower()=="sqlserver")
{
SqlDataAdapter adapter = new SqlDataAdapter();
cm.CommandText=sql;
adapter.SelectCommand=(SqlCommand)cm;
adapter.Fill(dt);
}
else
{
OleDbDataAdapter adapter = new OleDbDataAdapter();
cm.CommandText=sql;
adapter.SelectCommand=(OleDbCommand)cm;
adapter.Fill(dt);
}
return dt;
}
public IDataReader getdr(string sql)
{
cm.CommandText=sql;
return cm.ExecuteReader();
}
public string constr
{
get{return ConfigurationSettings.AppSettings["cnstr"];}
}
public System.Data.IDbCommand command(string sql)
{
cm.CommandText=sql;
return cm;
}
}
}
转自:http://ent.omeweb.com/book/content.aspx?id=1802
程序的演示地址为:http://ent.omeweb.com/catalog/index.html