asp.net 操作数据库最简方法- -

从asp 转到asp.net 的数据操作上,微软在设计上给了我们更多可选择余地. 但是,麻烦随之而来,出现便多语句需要我们去写.所以我对最常用的DataReader和Js 进行封装,得到现在这个DataJs.dll 可以做为数据库操作基础类进行应用,目前只支持Access 和SqlServer 两个数据库.

因为能力水下有限,现在将其源码公布只做互相学习之用(下面有应用实例),同时希望各位高手能给予指点~ ! 更加欢迎来 银蛇工作室(QQ群:6834017) 讨论指教.

主要包括三部分内容:1.数库连接字符串写法,2.DataJs.dll 源码 , 3. 实例aspx文件;

一、数库库连接

数库连接可写在webconfig.conf 配置文件中。如下:





每个 代表一个数据。 可任意多个。

 

二、 DataJs.dll 文件源码(C#)

using System;
using System.Data;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;
using System.Text;

namespace DataJs
{

 

public class Tado:System.ComponentModel.Component
{ public OleDbConnection conn;
public OleDbCommand cmd;
public System.Data.OleDb.OleDbDataReader rd;

private string getConnString(string connName)
{ //webconfig.conf 文件中设置参数顺序为固定: 数据库类型0,数据库名1,帐号2,密码3,服务器IP4
StringBuilder t=new StringBuilder();
if (HttpContext.Current.Application["connstr"]==null)
{ string []tmp =new string [5]; //read Data connection string from webconf.conf file.
tmp=System.Configuration.ConfigurationSettings.AppSettings[connName].Split(',');

if (tmp.Length!=5)
{ HttpContext.Current.Response.Write("webConfig.conf 配置出错! 缺少必要参数!");
HttpContext.Current.Response.End();
}


//对不同连接字符串进行处理
switch (tmp[0])
{ case "Access":
t.Append("user id=");t.Append(tmp[2]);
t.Append(";password=");t.Append(tmp[3]);
t.Append(";PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=");
t.Append(HttpContext.Current.Server.MapPath(tmp[1]));
t.Append(";persist security info=True");
break;

default: // SqlServer
t.Append("user id=");t.Append(tmp[2]);
t.Append(";password=");t.Append(tmp[3]);
t.Append(";Provider=SQLOLEDB.1;data source=");t.Append(tmp[4]);
t.Append(";persist security info=True;Initial Catalog=");t.Append(tmp[1]);
break;
}
HttpContext.Current.Application[connName]=t.ToString();
}
return @HttpContext.Current.Application[connName].ToString();
}

private void init(string connName)
{ conn= new OleDbConnection();
cmd=new OleDbCommand();
conn.ConnectionString=getConnString(connName);
conn.Open();
cmd.CommandType=CommandType.Text;
cmd.Connection=conn;
}


public Tado()
{
this.init("conn1");
}

public Tado(string connName)
{
this.init(connName);
}


public void ExecuteSql(string sqlstr)
{ cmd.CommandType=CommandType.Text;
cmd.CommandText=sqlstr;
cmd.ExecuteNonQuery();
}

public void CreateRd(string sqlstr)
{ cmd.CommandText=sqlstr;
rd=cmd.ExecuteReader();
}

public string ReturnValue(string sqlstr)
{ cmd.CommandType=CommandType.Text;
cmd.CommandText=sqlstr;
try
{
return cmd.ExecuteScalar().ToString();
}
catch {return "0";}
}

protected override void Dispose(bool disposing)
{
this.cmd = null;
if ((this.rd != null) && !this.rd.IsClosed) this.rd.Close();
this.rd = null;
if (this.conn.State == ConnectionState.Open) this.conn.Close();
this.conn = null;
}
}

public class Js:Control
{ public static void ExecuteJs(string str)
{ StringBuilder t=new StringBuilder();
t.Append(" ");
HttpContext.Current.Response.Write(t.ToString());
}

public static void Msgbox(string MsgStr)
{ StringBuilder t=new StringBuilder();
t.Append(" ");
HttpContext.Current.Response.Write(t.ToString());
HttpContext.Current.Response.End();
}

public static void Msgbox(string MsgStr,string UrlStr)
{ StringBuilder t=new StringBuilder();
t.Append(" ");
HttpContext.Current.Response.Write(t.ToString());
HttpContext.Current.Response.End();
}

public static void Msgbox(string MsgStr,string yesUrl,string noUrl)
{ StringBuilder t=new StringBuilder();
t.Append(" ");
HttpContext.Current.Response.Write(t.ToString());
HttpContext.Current.Response.End();
}

public static void print(string MsgStr)
{
HttpContext.Current.Response.Write(MsgStr);
}

public static void debug(string MsgStr)
{
HttpContext.Current.Response.Write(MsgStr);
HttpContext.Current.Response.End();
}
}//end class
}//end nameSpace

 

版权归 银蛇工作室(QQ群:6834017) BlueLeopard 所有. 转载请注明出处! 谢谢~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值