您的位置: 旅游网 > 社会

在ASP.NET程序中创建唯一序号

发布时间:2019-09-13 19:19:24

附:OleDbWrap.cs ,是一个很简单的封装类,封装了基本的数据库操作。

using System;

using System.Data;

using System.Data.OleDb;

namespace Wyy.Wrap

{

/// <summary>

/// wDbWrap 说明:完成数据库访问功能

/// 1 创建Connection,Adapter,管理对象内的Conn和Adapter对象

/// 2 通过Select结果集填充Dataset

/// 3 插入

/// 4 更新

/// </summary>

public class OleDbWrap

{

//--------公共方法

/// <summary>

/// 在对象被清除时会自动清除 数据库连接对象

/// </summary>

public OleDbWrap()

{

m_dbConn = new OleDbConnection(DbString);

m_fAutoDelConn = true;

m_dbAdapter = new OleDbDataAdapter();

m_dbConn.Open();

}

/// <summary>

/// 通过连接字符串构造内部的数据库连接对象

/// </summary>

/// <param name="strConnection">ADO连接字符串</param>

public OleDbWrap(string strConnection)

{

m_dbConn = new OleDbConnection(strConnection);

m_fAutoDelConn = true;

m_dbAdapter = new OleDbDataAdapter();

m_dbConn.Open();

}

/// <summary>

/// 通过现有连接构造对象,在对象被清除时不会自动清除 数据库连接对象

/// </summary>

/// <param name="conn">现存的数据库连接对象</param>

public OleDbWrap(OleDbConnection conn)

{

m_dbConn = conn;

m_fAutoDelConn = false;

m_dbAdapter = new OleDbDataAdapter();

//m_dbConn.Open();

}

public virtual void Dispose()

{

m_dbAdapter.Dispose();

if(m_fAutoDelConn)

{

m_dbConn.Close();

m_dbConn.Dispose();

}

}

/// <summary>

/// 通过SQL语句创建DataReader对象

/// </summary>

/// <param name="szSql">SQL语句</param>

/// <returns>DataReader对象</returns>

public OleDbDataReader CreateDataReader(string szSql)

{

OleDbCommand cmd = new OleDbCommand(szSql,m_dbConn);

OleDbDataReader dr= cmd.ExecuteReader();

cmd.Dispose();

return dr;

}

/// <summary>

/// 通过SQL查询语句,返回第一行结果,可以用于执行类似与Select Count(*)的语句

/// </summary>

/// <param name="szSql">SQL语句</param>

/// <returns>返回对象</returns>

public object ExecuteScalar(string szSql)

{

OleDbCommand idCMD = new OleDbCommand(szSql, m_dbConn);

object rObj = idCMD.ExecuteScalar();

idCMD.Dispose();

return rObj;

}

/// <summary>

/// 调用OleDbCommand 的 ExecuteNonQuery

/// </summary>

/// <param name="szSql"></param>

/// <returns></returns>

public int ExecuteNonQuery(string szSql)

{

OleDbCommand idCMD = new OleDbCommand(szSql, m_dbConn);

int iRet = idCMD.ExecuteNonQuery();

idCMD.Dispose();

return iRet;

}

/// <summary>

/// 创建查询用DataSet对象

/// </summary>

/// <param name="szSql">查询SQL语句</param>

/// <param name="szTabName">表名称</param>

/// <returns>已经被填充的DataSet对象</returns>

public DataSet CreateSelectDataSet(string szSql,string szTabName)

{

m_dbAdapter.SelectCommand = new OleDbCommand(szSql,m_dbConn);

DataSet ds = new DataSet();

m_dbAdapter.Fill(ds,szTabName);

return ds;

}

/// <summary>

/// 通过查询语句 创建DataSet对象,返回的对象用于执行插入操作

/// </summary>

/// <param name="szSelectSql">SQL语句</param>

/// <param name="szTabName">表名称</param>

/// <returns>用于插入的DataSet对象</returns>

public DataSet CreateInsertDataSet_bySelect(string szSelectSql,string szTabName)

{

m_dbAdapter.SelectCommand = new OleDbCommand(szSelectSql, m_dbConn);

OleDbCommandBuilder cb = new OleDbCommandBuilder(m_dbAdapter);

DataSet ds = new DataSet();

m_dbAdapter.Fill(ds, szTabName);

m_dbAdapter.RowUpdated += new OleDbRowUpdatedEventHandler(OnRowUpdated_Inserted);

return ds;

}

/// <summary>

/// 通过查询语句 创建DataSet对象,返回的对象用于执行更新操作

/// </summary>

/// <param name="szSelectSql">SQL语句</param>

/// <param name="szTabName">表名称</param>

/// <returns>用于更新的DataSet对象</returns>

public DataSet CreateUpdateDataSet_bySelect(string szSelectSql,string szTabName)

{

m_dbAdapter.SelectCommand = new OleDbCommand(szSelectSql, m_dbConn);

OleDbCommandBuilder cb = new OleDbCommandBuilder(m_dbAdapter);

DataSet ds = new DataSet();

m_dbAdapter.Fill(ds, szTabName);

return ds;

//m_dbAdapter.RowUpdated += new OleDbRowUpdatedEventHandler(OnRowUpdated_Update);

}

//----------------私有事件处理

/// <summary>

/// 处理数据插入的更新事件

/// </summary>

/// <param name="sender"></param>

/// <param name="args"></param>

protected void OnRowUpdated_Inserted(object sender, OleDbRowUpdatedEventArgs args)

{

OleDbCommand idCMD = new OleDbCommand("SELECT @@IDENTITY", m_dbConn);

if (args.StatementType == StatementType.Insert)

{

object rObj = idCMD.ExecuteScalar();

if(rObj == null)

m_iDbIdentity =-1;

else if( rObj.ToString() !="")

m_iDbIdentity = Int32.Parse(rObj.ToString());

else

m_iDbIdentity =-1;

}

idCMD.Dispose();

}

//------------公共属性

/// <summary>

/// 在插入数据后获取新数据行中自增字段的值,目前只能支持一个自增字段

/// </summary>

public Int32 DbIdentity {get{return m_iDbIdentity;} }

/// <summary>

/// 数据库连接字符串,保存在web.config文件中 <appSettings>节

/// </summary>

public string DbString {get{return System.Configuration.ConfigurationSettings.AppSettings["dbStr"];}}

//------------公共变量

/// <summary>

/// 数据库连接

/// </summary>

public OleDbConnection m_dbConn;

/// <summary>

/// 查询Adapter

/// </summary>

public OleDbDataAdapter m_dbAdapter;

public const String m_szRootUrl ="/copathway/toDo/";

//---------- 私有变量

/// <summary>

/// 保存数据库插入是自增字段的值

/// </summary>

protected Int32 m_iDbIdentity =-1;

protected bool m_fAutoDelConn = true;

}

} 查看本文来源

上腹胀的中医治疗
小孩流鼻血是怎么回事
糖尿病胃轻瘫消化不好的治疗方法
男人夜晚尿频是什么原因
猜你会喜欢的
猜你会喜欢的