using System; using System.Data; using System.Configuration; using System.Data.SqlClient; using System.Data.OleDb; using IBMDADB2Lib; using HR1930.COM.Util; namespace HR1930.COM.DB { /// <summary> /// Summary description for ADOHelper. /// </summary> public class ADOHelper {
// private member variables OleDbConnection con; static String m_ConnectionString; public ADOHelper() { con = new OleDbConnection(ADOHelper.ConnectionString());
} public DataSet ExecuteGet(string cmd) { this.CheckConnection();
DataSet dataSet = new DataSet(); try { OleDbCommand dataCommand = new OleDbCommand(cmd,con); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(); dataAdapter.SelectCommand = dataCommand;
dataAdapter.Fill(dataSet, "recordSet");
} catch(SqlException se) { ErrorLog el = new ErrorLog(se); throw new Exception("Error in SQL", se); } this.Dispose(); return dataSet; } public OleDbDataReader ExecuteRead(String cmd) { this.CheckConnection(); OleDbDataReader dr = null;
try { OleDbCommand dc = new OleDbCommand(cmd, con); dr = dc.ExecuteReader(); return dr; } catch(SqlException se) { ErrorLog el = new ErrorLog(se); } this.Dispose(); return dr; } public void ExecuteUpdate(string cmd) { this.CheckConnection(); try { OleDbCommand dc = new OleDbCommand(cmd, con); dc.ExecuteNonQuery(); } catch(SqlException se) { ErrorLog el = new ErrorLog(se); } this.Dispose(); return; } private void CheckConnection() { try { if (con.State != ConnectionState.Open) con.Open(); } catch (System.Data.SqlClient.SqlException se) { ErrorLog el = new ErrorLog(se); throw new Exception("Failed to Open connection.", se); } } public static String ConnectionString() { // Pull the ConnectionString from the ASP+ AppSettings section. // Cache in static field for faster repeat access. if (m_ConnectionString == null) { m_ConnectionString = (String) ConfigurationSettings.AppSettings["ConnectionString"];
if (m_ConnectionString == null) { throw new Exception("Connect string value not set in Web.config"); } } return m_ConnectionString;
} public void Dispose() { try { if (con.State == ConnectionState.Open) con.Close(); //con = null; } catch(Exception e) { ErrorLog el = new ErrorLog(e);
web.config文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<appSettings>
<add key="DataConnectionString" value="PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\Mydata\Mydata.mdb"/>
</appSettings>
</configuration>*.aspx文件:
<script runat="server">
protected void Page_Load(Object Src, EventArgs E)
{
if(!IsPostBack)
{
OleDbConnection ConnctingString = new OleDbConnection(ConfigurationSettings.AppSettings["DataConnectionString"]);
try
{
HotalConnctingString.Open();
OleDbCommand Comm = new OleDbCommand("select * from tb", ConnctingString);
MyDataGrid1.DataSource = Comm.ExecuteReader();
MyDataGrid1.DataBind();
Comm.Dispose();
}
catch (OleDbException ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
ConnctingString.Close();
}
}
}
</script>
<form runat=server>
<asp:DataGrid ID="MyDataGrid1" runat =server />
</form>
小弟菜鸟,还请多指教!!
小弟菜鸟,还请多指教!!
---------------------------------
我说过了--
一般情况下数据访问的操作写在单独的类文件中的,数据库的连接也包括在其中,在需要进行数据库操作的时候,事例化该类,就可以了.
---------------
需要进行数据库操作的时候,事例化该类,直接打开已经建立的连接就可以了(若原来没有建立那就建立,这一步是在数据库防卫类里实现的.如
/// <summary>
/// //打开数据库
/// </summary>
public void Open()
{
if (conn==null)//若没有建立则建立连接
{
conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings["connBaoZang"]);
}
if (conn.State==System.Data.ConnectionState.Closed)若关闭了就打开它
{
try
{
conn.Open();
}
catch (SqlException Ex1)
{
System.Web.HttpContext.Current.Response.Write("<br/>SqlErr:"+Ex1.ToString());
System.Web.HttpContext.Current.Response.End();
}
}}
建议楼主看写基础的东东好一些.
<appSettings>
<add key="ConnectString" value="user id=cretnet;password=cretnet;initial catalog=CretNet;data source=10.71.36.5\CRETSOFT;Connect Timeout=30"></add>
<add key="DatabaseBackupPath" value="C:\"></add>
</appSettings>在global.asax中
protected void Application_Start(Object sender, EventArgs e)
{
Application["ConnectString"] = ConfigurationSettings.AppSettings["ConnectString"];
}
<appSettings>
<add key="ConnectString" value="server=(local);user id=sa;password=;Connect Timeout=30"/〉
</appSettings>
对数据库的操作可以封装到一个类里,用的时候可以直接调用
<appSettings>
<add key="ConnectString" value="server=(local);database=pubs;user id=sa;password=;Connect Timeout=30"/〉
</appSettings>
对数据库的操作可以封装到一个类里,用的时候可以直接调用
http://www.mscenter.edu.cn/laputa/article/2004-12/0/13/10316.xml
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OleDb;
using IBMDADB2Lib;
using HR1930.COM.Util;
namespace HR1930.COM.DB
{
/// <summary>
/// Summary description for ADOHelper.
/// </summary>
public class ADOHelper
{
// private member variables
OleDbConnection con;
static String m_ConnectionString; public ADOHelper()
{
con = new OleDbConnection(ADOHelper.ConnectionString());
}
public DataSet ExecuteGet(string cmd)
{
this.CheckConnection();
DataSet dataSet = new DataSet(); try
{
OleDbCommand dataCommand = new OleDbCommand(cmd,con);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = dataCommand;
dataAdapter.Fill(dataSet, "recordSet");
}
catch(SqlException se)
{
ErrorLog el = new ErrorLog(se);
throw new Exception("Error in SQL", se);
}
this.Dispose();
return dataSet;
}
public OleDbDataReader ExecuteRead(String cmd)
{
this.CheckConnection(); OleDbDataReader dr = null;
try
{
OleDbCommand dc = new OleDbCommand(cmd, con);
dr = dc.ExecuteReader();
return dr;
}
catch(SqlException se)
{
ErrorLog el = new ErrorLog(se);
}
this.Dispose();
return dr;
}
public void ExecuteUpdate(string cmd)
{
this.CheckConnection();
try
{
OleDbCommand dc = new OleDbCommand(cmd, con);
dc.ExecuteNonQuery();
}
catch(SqlException se)
{
ErrorLog el = new ErrorLog(se);
}
this.Dispose();
return;
}
private void CheckConnection()
{
try
{
if (con.State != ConnectionState.Open)
con.Open();
}
catch (System.Data.SqlClient.SqlException se)
{
ErrorLog el = new ErrorLog(se);
throw new Exception("Failed to Open connection.", se);
}
}
public static String ConnectionString()
{
// Pull the ConnectionString from the ASP+ AppSettings section.
// Cache in static field for faster repeat access.
if (m_ConnectionString == null)
{
m_ConnectionString = (String) ConfigurationSettings.AppSettings["ConnectionString"];
if (m_ConnectionString == null)
{
throw new Exception("Connect string value not set in Web.config");
}
}
return m_ConnectionString;
} public void Dispose()
{
try
{
if (con.State == ConnectionState.Open)
con.Close();
//con = null;
}
catch(Exception e)
{
ErrorLog el = new ErrorLog(e);
}
}
}
}