封装的ADO.NET对数据库操作经典类

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Remoting.Messaging;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace LTP.SQLServerDAL
... {
/**////<summary>
///ADO.NET数据库操作基础类。
///</summary>

publicabstractclassDbManagerSQL
...{
//数据库连接字符串
protectedstaticstringconnectionString=ConfigurationSettings.AppSettings["ConnectionString"];
publicDbManagerSQL()
...{
//
//TODO:在此处添加构造函数逻辑
//
}

/**////<summary>
///执行SQL语句,返回影响的记录数
///</summary>
///<paramname="SQLString"></param>
///<returns></returns>

publicstaticintExecuteSql(stringSQLString)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
using(SqlCommandcmd=newSqlCommand(SQLString,connection))
...{
try
...{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}

catch(System.Data.SqlClient.SqlExceptionE)
...{
thrownewException(E.Message);
}

}

}

}

/**////<summary>
///执行两条SQL语句,实现数据库事务。
///</summary>
///<paramname="SQLString1"></param>
///<paramname="SQLString2"></param>

publicstaticvoidExecuteSqlTran(stringSQLString1,stringSQLString2)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
connection.Open();
SqlCommandcmd
=newSqlCommand();
cmd.Connection
=connection;
SqlTransactiontx
=connection.BeginTransaction();
cmd.Transaction
=tx;
try
...{
cmd.CommandText
=SQLString1;
cmd.ExecuteNonQuery();
cmd.CommandText
=SQLString2;
cmd.ExecuteNonQuery();
tx.Commit();
}

catch(System.Data.SqlClient.SqlExceptionE)
...{
tx.Rollback();
thrownewException(E.Message);
}

finally
...{
cmd.Dispose();
connection.Close();
}

}

}

/**////<summary>
///执行多条SQL语句,实现数据库事务,每条语句以“;”分割。
///</summary>
///<paramname="SQLStringList"></param>

publicstaticvoidExecuteSqlTran(stringSQLStringList)
...{
using(OdbcConnectionconn=newOdbcConnection(connectionString))
...{
conn.Open();
OdbcCommandcmd
=newOdbcCommand();
cmd.Connection
=conn;
OdbcTransactiontx
=conn.BeginTransaction();
cmd.Transaction
=tx;
try
...{
string[]split=SQLStringList.Split(newChar[]...{';'});
foreach(stringstrsqlinsplit)
...{
if(strsql.Trim()!="")
...{
cmd.CommandText
=strsql;
cmd.ExecuteNonQuery();
}

}

tx.Commit();
}

catch(System.Data.Odbc.OdbcExceptionE)
...{
tx.Rollback();
thrownewException(E.Message);
}

}

}

/**////<summary>
///执行带一个存储过程参数的的SQL语句。
///</summary>
///<paramname="SQLString"></param>
///<paramname="content"></param>
///<returns></returns>

publicstaticintExecuteSql(stringSQLString,stringcontent)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
SqlCommandcmd
=newSqlCommand(SQLString,connection);
System.Data.SqlClient.SqlParametermyParameter
=newSystem.Data.SqlClient.SqlParameter("@content",SqlDbType.NText);
myParameter.Value
=content;
cmd.Parameters.Add(myParameter);
try
...{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}

catch(System.Data.SqlClient.SqlExceptionE)
...{
thrownewException(E.Message);
}

finally
...{
cmd.Dispose();
connection.Close();
}

}

}

/**////<summary>
///向数据库里插入图像格式的字段
///</summary>
///<paramname="strSQL"></param>
///<paramname="fs"></param>
///<returns></returns>

publicstaticintExecuteSqlInsertImg(stringstrSQL,byte[]fs)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
SqlCommandcmd
=newSqlCommand(strSQL,connection);
System.Data.SqlClient.SqlParametermyParameter
=newSystem.Data.SqlClient.SqlParameter("@fs",SqlDbType.Image);
myParameter.Value
=fs;
cmd.Parameters.Add(myParameter);
try
...{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}

catch(System.Data.SqlClient.SqlExceptionE)
...{
thrownewException(E.Message);
}

finally
...{
cmd.Dispose();
connection.Close();
}


}

}

/**////<summary>
///执行一条计算查询结果语句,返回查询结果(整数)。
///</summary>
///<paramname="strSQL"></param>
///<returns></returns>

publicstaticintGetCount(stringstrSQL)
...{
using(SqlConnectionconnection=newSqlConnection(connectionString))
...{
SqlCommandcmd
=newSqlCommand(strSQL,connection);
try
...{
connection.Open();
SqlDataReaderresult
=cmd.ExecuteReader();
inti=0;
while(result.Read())
...{
i
=result.GetInt32(0);
}

result.Close();
returni;
}

catch(System.Data.SqlClient.SqlExceptione)
...{
thrownewException(e.Message);
}

finally
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值