仿照以前收集的一个经典sql server数据访问类,稍做修改。
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.data.oledb;
/// <summary>
/// dataaccess 的摘要说明
/// </summary>
public class dataaccess
{
protected static oledbconnection conn = new oledbconnection();
protected static oledbcommand comm = new oledbcommand();
public dataaccess()
{
//init
}
private static void openconnection()
{
if (conn.state == connectionstate.closed)
{
conn.connectionstring = @"provider=microsoft.jet.oledb.4.0;data source="+configurationmanager.appsettings["myconn"];//web.config文件里设定。
comm.connection = conn;
try
{
conn.open();
}
catch (exception e)
{ throw new exception(e.message); }
}
}//打开数据库
private static void closeconnection()
{
if (conn.state == connectionstate.open)
{
conn.close();
conn.dispose();
comm.dispose();
}
}//关闭数据库
public static void excutesql(string sqlstr)
{
try
{
openconnection();
comm.commandtype = commandtype.text;
comm.commandtext = sqlstr;
comm.executenonquery();
}
catch (exception e)
{
throw new exception(e.message);
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器