DbAccess.cs:
using System;
using System.Data;
using System.Data.SqlClient;
using SoftDream.Tools.util;namespace SoftDream.Tools.db
{
public class DbAccess:IDisposable
{
private SqlConnection _connection;
private SqlTransaction _transaction;
private SqlCommand sqlCmd;
private SqlDataAdapter sqlDA;
private DataSet dataSet;
private DataRow dataRow;
private string sqlStmt;
protected void InitConnection()
{
_connection = ConnectManager.getConnect();
} public DbAccess()
{
InitConnection();
}
protected internal virtual IDataReader ExecuteReader(IDbCommand command)
{
return command.ExecuteReader();
}
internal IDbDataParameter AddParameter(IDbCommand cmd, string paramName,
DbType dbType, object value)
{
IDbDataParameter parameter = cmd.CreateParameter();
parameter.ParameterName = CreateCollectionParameterName(paramName);
parameter.DbType = dbType;
parameter.Value = null == value ? DBNull.Value : value;
cmd.Parameters.Add(parameter);
return parameter;
}
public IDbTransaction BeginTransaction()
{
CheckTransactionState(false);
_transaction = _connection.BeginTransaction();
return _transaction;
}
public IDbTransaction BeginTransaction(IsolationLevel isolationLevel)
{
CheckTransactionState(false);
_transaction = _connection.BeginTransaction(isolationLevel);
return _transaction;
}
public void CommitTransaction()
{
CheckTransactionState(true);
_transaction.Commit();
_transaction = null;
}
public void RollbackTransaction()
{
CheckTransactionState(true);
_transaction.Rollback();
_transaction = null;
}
private void CheckTransactionState(bool mustBeOpen)
{
if(mustBeOpen)
{
if(null == _transaction)
throw new InvalidOperationException("Transaction is not open.");
}
else
{
if(null != _transaction)
throw new InvalidOperationException("Transaction is already open.");
}
}
internal IDbCommand CreateCommand(string sqlText)
{
return CreateCommand(sqlText, false);
}
internal IDbCommand CreateCommand(string sqlText, bool procedure)
{
IDbCommand cmd = _connection.CreateCommand();
cmd.CommandText = sqlText;
cmd.Transaction = _transaction;
if(procedure)
cmd.CommandType = CommandType.StoredProcedure;
return cmd;
}
public virtual void Close()
{
if(null != _connection)
_connection.Close();
}
protected internal string CreateSqlParameterName(string paramName)
{
return "@" + paramName;
}
protected string CreateCollectionParameterName(string baseParamName)
{
return "@" + baseParamName;
}
public SqlConnection Connection
{
get { return _connection; }
} public virtual void Dispose()
{
Close();
if(null != _connection)
_connection.Dispose();
}多谢。
using System;
using System.Data;
using System.Data.SqlClient;
using SoftDream.Tools.util;namespace SoftDream.Tools.db
{
public class DbAccess:IDisposable
{
private SqlConnection _connection;
private SqlTransaction _transaction;
private SqlCommand sqlCmd;
private SqlDataAdapter sqlDA;
private DataSet dataSet;
private DataRow dataRow;
private string sqlStmt;
protected void InitConnection()
{
_connection = ConnectManager.getConnect();
} public DbAccess()
{
InitConnection();
}
protected internal virtual IDataReader ExecuteReader(IDbCommand command)
{
return command.ExecuteReader();
}
internal IDbDataParameter AddParameter(IDbCommand cmd, string paramName,
DbType dbType, object value)
{
IDbDataParameter parameter = cmd.CreateParameter();
parameter.ParameterName = CreateCollectionParameterName(paramName);
parameter.DbType = dbType;
parameter.Value = null == value ? DBNull.Value : value;
cmd.Parameters.Add(parameter);
return parameter;
}
public IDbTransaction BeginTransaction()
{
CheckTransactionState(false);
_transaction = _connection.BeginTransaction();
return _transaction;
}
public IDbTransaction BeginTransaction(IsolationLevel isolationLevel)
{
CheckTransactionState(false);
_transaction = _connection.BeginTransaction(isolationLevel);
return _transaction;
}
public void CommitTransaction()
{
CheckTransactionState(true);
_transaction.Commit();
_transaction = null;
}
public void RollbackTransaction()
{
CheckTransactionState(true);
_transaction.Rollback();
_transaction = null;
}
private void CheckTransactionState(bool mustBeOpen)
{
if(mustBeOpen)
{
if(null == _transaction)
throw new InvalidOperationException("Transaction is not open.");
}
else
{
if(null != _transaction)
throw new InvalidOperationException("Transaction is already open.");
}
}
internal IDbCommand CreateCommand(string sqlText)
{
return CreateCommand(sqlText, false);
}
internal IDbCommand CreateCommand(string sqlText, bool procedure)
{
IDbCommand cmd = _connection.CreateCommand();
cmd.CommandText = sqlText;
cmd.Transaction = _transaction;
if(procedure)
cmd.CommandType = CommandType.StoredProcedure;
return cmd;
}
public virtual void Close()
{
if(null != _connection)
_connection.Close();
}
protected internal string CreateSqlParameterName(string paramName)
{
return "@" + paramName;
}
protected string CreateCollectionParameterName(string baseParamName)
{
return "@" + baseParamName;
}
public SqlConnection Connection
{
get { return _connection; }
} public virtual void Dispose()
{
Close();
if(null != _connection)
_connection.Dispose();
}多谢。
数据操作类
顶部 using DbAccess
然后 在你需要 进行 数据操作的部分 将
DbAccess db=new DbAccess(); //实例化
然后 调用DbAccess相应的方法 就是
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SoftDream.Tools.db;
using SoftDream.Tools.util;
using IRM.Base.Cache;
using IRM.Base;
namespace IRM.Admin
{
/// <summary>
/// history 的摘要说明。
/// </summary>
public class history : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox txt_mgid;
protected System.Web.UI.WebControls.TextBox txt_title;
protected System.Web.UI.WebControls.TextBox txt_memo;
protected System.Web.UI.WebControls.TextBox txt_adddate;
protected System.Web.UI.WebControls.TextBox txt_enddate;
protected System.Web.UI.WebControls.DataGrid grid1;
DataSet ds=new DataSet();
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
DbAccess db = null;
try
{
DataSet ds=null;
string sql="select mg_id,title,adddate,enddate from notes where datediff(day,getdate(),enddate)<=0";
db=new DbAccess();
ds=db.ExecuteQuery(sql);
grid1.DataSource=ds;
grid1.DataBind();
}
catch(SqlException a)
{
Response.Write(a.ToString());
}
finally
{
if(db!=null)
{
db.Close();
}
}
protected void OnClick(object sender,EventArgs e)
{
string mg_id=txt_mgid.Text;
DataRow row=ds.Tables[0].Rows.Find(mg_id);
row["title"]=txt_title.Text;
row["memo"]=txt_memo.Text;
row["adddate"]=txt_adddate.Text;
row["enddate"]=txt_enddate.Text;
DbAccess Sqlpter=new DbAccess();
Sqlpter.UpdateRow (ds.Tables[0]);
ds.Tables[0].AcceptChanges();
Table1.Visible=false;
grid1.DataSource=ds;
grid1.DataBind();
}
public void OnItemCommand(object sender,DataGridCommandEventArgs e)
{
if(((Button)e.CommandSource).CommandName=="Modify")
{
Table1.Visible=true;
txt_mgid.Text=e.Item.Cells[0].Text;
txt_title.Text=e.Item.Cells[1].Text;
txt_memo.Text=e.Item.Cells[2].Text;
txt_adddate.Text=e.Item.Cells[3].Text;
txt_enddate.Text=e.Item.Cells[4].Text;
}
else if(((Button)e.CommandSource).CommandName=="Delete")
{
Table1.Visible=false;
string mg_id=e.Item.Cells[0].Text;
DataRow row=ds.Tables[0].Rows.Find(mg_id);
row.Delete(); DbAccess Sqlpter=new DbAccess();
Sqlpter.UpdateRow(ds.Tables[0]);
ds.Tables[0].AcceptChanges();
Table1.Visible=true;
grid1.DataSource=ds;
grid1.DataBind();
}
}
但是一运行就报错:E:\IRM\Admin\User\history.aspx.cs(210): 重载“UpdateRow”方法未获取“1”参数,这是什么原因,还有我改过的代码能实现上面那些功能吗?请高手前来指点!!
updaterow 方法有重载,你调用的那个不只一个参数,所以错误