网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 交易代码 > SQL语法 > 正文

DbHelperSQL

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务
?usingSystem;
    usingSystem.Collections.Generic;
    usingSystem.Text;
    usingSystem.Collections;
    usingSystem.Collections.Specialized;
    usingSystem.Data;
    usingSystem.Data.SqlClient;
    usingSystem.Configuration;
    usingSystem.Web;
    namespaceHengmyj
    {
    publicabstractclassDbHelperSQL
    {
    //protectedstaticstringconnectionString=ConfigurationManager.AppSettings["ConnectionString"];
    protectedstaticstringconnectionString=PubConstant.ConnectionString;
    //WebConfig中写入节点中
    //
    //
    publicDbHelperSQL()
    {
    }
    #region公用方法
    
    publicstaticintGetMaxID(stringFieldName,stringTableName)
    {
    stringstrsql="selectmax("+FieldName+")+1from"+TableName;
    objectobj=GetSingle(strsql);
    if(obj==null)
    {
    return1;
    }
    else
    {
    returnint.Parse(obj.ToString());
    }
    }
    publicstaticboolExists(stringstrSql,paramsSqlParameter[]cmdParms)
    {
    objectobj=GetSingle(strSql,cmdParms);
    intcmdresult;
    if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
    {
    cmdresult=0;
    }
    else
    {
    cmdresult=int.Parse(obj.ToString());
    }
    if(cmdresult==0)
    {
    returnfalse;
    }
    else
    {
    returntrue;
    }
    }
    #endregion
    #region执行简单SQL语句
    ///
    ///执行SQL语句,返回影响的记录数
    ///

    ///SQL语句
    ///影响的记录数
    publicstaticintExecuteSql(stringSQLString)
    {
    using(SqlConnectionconnection=newSqlConnection(connectionString))
    {
    using(SqlCommandcmd=newSqlCommand(SQLString,connection))
    {
    try
    {
    connection.Open();
    introws=cmd.ExecuteNonQuery();
    returnrows;
    }
    catch(System.Data.SqlClient.SqlExceptionE)
    {
    connection.Close();
    thrownewException(E.Message);
    }
    }
    }
    }
    ///
    ///执行多条SQL语句,实现数据库事务。
    ///

    ///多条SQL语句
    publicstaticvoidExecuteSqlTran(ArrayListSQLStringList)
    {
    using(SqlConnectionconn=newSqlConnection(connectionString))
    {
    conn.Open();
    SqlCommandcmd=newSqlCommand();
    cmd.Connection=conn;
    SqlTransactiontx=conn.BeginTransaction();
    cmd.Transaction=tx;
    try
    {
    for(intn=0;n    {
    stringstrsql=SQLStringList[n].ToString();
    if(strsql.Trim().Length>1)
    {
    cmd.CommandText=strsql;
    cmd.ExecuteNonQuery();
    }
    }
    tx.Commit();
    }
    catch(System.Data.SqlClient.SqlExceptionE)
    {
    tx.Rollback();
    thrownewException(E.Message);
    }
    }
    }
    ///
    ///执行带一个存储过程参数的的SQL语句。
    ///

    ///SQL语句
    ///参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加
    ///影响的记录数
    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();
    }
    }
    }
    ///
    ///向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
    ///

    ///SQL语句
    ///图像字节,数据库的字段类型为image的情况
    ///影响的记录数
    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();
    }
    }
    }
    ///
    ///执行一条计算查询结果语句,返回查询结果(object)。
    ///

    ///计算查询结果语句
    ///查询结果(object)
    publicstaticobjectGetSingle(stringSQLString)
    {
    using(SqlConnectionconnection=newSqlConnection(connectionString))
    {
    using(SqlCommandcmd=newSqlCommand(SQLString,connection))
    {
    try
    {
    try
    {
    connection.Open();
    }
    catch(System.Data.SqlClient.SqlExceptionse)
    {
    thrownewException(se.Message);
    //returnnull;
    }
    objectobj=cmd.ExecuteScalar();
    if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
    {
    return"";
    }
    else
    {
    returnobj;
    }
    }
    catch(System.Data.SqlClient.SqlExceptione)
    {
    connection.Close();
    thrownewException(e.Message);
    }
    }
    }
    }
    ///
    ///执行查询语句,返回SqlDataReader
    ///

    ///查询语句
    ///SqlDataReader
    publicstaticSqlDataReaderExecuteReader(stringstrSQL)
    {
    SqlConnectionconnection=newSqlConnection(connectionString);
    SqlCommandcmd=newSqlCommand(strSQL,connection);
    //cmd.set
    try
    {
    connection.Open();
    SqlDataReadermyReader=cmd.ExecuteReader();
    returnmyReader;
    }
    catch(System.Data.SqlClient.SqlExceptione)
    {
    thrownewException(e.Message);
    }
    }
    ///
    ///执行查询语句,返回DataSet
    ///

    ///查询语句
    ///DataSet
    publicstaticDataSetQuery(stringSQLString)
    {
    using(SqlConnectionconnection=newSqlConnection(connectionString))
    {
    DataSetds=newDataSet();
    try
    {
    connection.Open();
    SqlDataAdaptercommand=newSqlDataAdapter(SQLString,connection);
    command.Fill(ds,"ds");
    }
    catch(System.Data.SqlClient.SqlExceptionex)
    {
    thrownewException(ex.Message);
    }
    returnds;
    }
    }
    ///
    ///列表
    ///

    ///
    ///
    ///
    ///
    publicstaticDataSetQuery(stringSQLString,intstart,intmaxinum)
    {
    using(SqlConnectionconnection=newSqlConnection(connectionString))
    {
    DataSetds=newDataSet();
    try
    {
    connection.Open();
    SqlDataAdaptercommand=newSqlDataAdapter(SQLString,connection);
    command.Fill(ds,start,maxinum,"ds");
    //command.Fill(ds,"ds");
    }
    catch(System.Data.SqlClient.SqlExceptionex)
    {
    thrownewException(ex.Message);
    }
    returnds;
    }
    }
    #endregion
    #region执行带参数的SQL语句
    ///
    ///执行SQL语句,返回影响的记录数
    ///

    ///SQL语句
    ///影响的记录数
    publicstaticintExecuteSql(stringSQLString,paramsSqlParameter[]cmdParms)
    {
    using(SqlConnectionconnection=newSqlConnection(connectionString))
    {
    using(SqlCommandcmd=newSqlCommand())
    {
    try
    {
    PrepareCommand(cmd,connection,null,SQLString,cmdParms);
    introws=cmd.ExecuteNonQuery();
    cmd.Parameters.Clear();
    returnrows;
    }
    catch(System.Data.SqlClient.SqlExceptionE)
    {
    thrownewException(E.Message);
    }
    }
    }
    }
    ///
    ///执行多条SQL语句,实现数据库事务。
    ///

    ///SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])
    publicstaticvoidExecuteSqlTran(HashtableSQLStringList)
    {
    using(SqlConnectionconn=newSqlConnection(connectionString))
    {
    conn.Open();
    using(SqlTransactiontrans=conn.BeginTransaction())
    {
    SqlCommandcmd=newSqlCommand();
    try
    {
    //循环
    foreach(DictionaryEntrymyDEinSQLStringList)
    {
    stringcmdText=myDE.Key.ToString();
    SqlParameter[]cmdParms=(SqlParameter[])myDE.Value;
    PrepareCommand(cmd,conn,trans,cmdText,cmdParms);
    intval=cmd.ExecuteNonQuery();
    cmd.Parameters.Clear();
    trans.Commit();
    }
    }
    catch
    {
    trans.Rollback();
    throw;
    }
    }
    }
    }
    ///
    ///执行一条计算查询结果语句,返回查询结果(object)。
    ///

    ///计算查询结果语句
    ///查询结果(object)
    publicstaticobjectGetSingle(stringSQLString,paramsSqlParameter[]cmdParms)
    {
    using(SqlConnectionconnection=newSqlConnection(connectionString))
    {
    using(SqlCommandcmd=newSqlCommand())
    {
    try
    {
    PrepareCommand(cmd,connection,null,SQLString,cmdParms);
    objectobj=cmd.ExecuteScalar();
    cmd.Parameters.Clear();
    if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
    {
    returnnull;
    }
    else
    {
    returnobj;
    }
    }
    catch(System.Data.SqlClient.SqlExceptione)
    {
    thrownewException(e.Message);
    }
    }
    }
    }
    ///
    ///执行查询语句,返回SqlDataReader
    ///

    ///查询语句
    ///SqlDataReader
    publicstaticSqlDataReaderExecuteReader(stringSQLString,paramsSqlParameter[]cmdParms)
    {
    SqlConnectionconnection=newSqlConnection(connectionString);
    SqlCommandcmd=newSqlCommand();
    try
    {
    PrepareCommand(cmd,connection,null,SQLString,cmdParms);
    SqlDataReadermyReader=cmd.ExecuteReader();
    cmd.Parameters.Clear();
    returnmyReader;
    }
    catch(System.Data.SqlClient.SqlExceptione)
    {
    thrownewException(e.Message);
    }
    }
    ///
    ///执行查询语句,返回DataSet
    ///

    ///查询语句
    ///DataSet
    publicstaticDataSetQuery(stringSQLString,paramsSqlParameter[]cmdParms)
    {
    using(SqlConnectionconnection=newSqlConnection(connectionString))
    {
    SqlCommandcmd=newSqlCommand();
    PrepareCommand(cmd,connection,null,SQLString,cmdParms);
    using(SqlDataAdapterda=newSqlDataAdapter(cmd))
    {
    DataSetds=newDataSet();
    try
    {
    da.Fill(ds,"ds");
    cmd.Parameters.Clear();
    }
    catch(System.Data.SqlClient.SqlExceptionex)
    {
    thrownewException(ex.Message);
    }
    returnds;
    }
    }
    }
    privatestaticvoidPrepareCommand(SqlCommandcmd,SqlConnectionconn,SqlTransactiontrans,stringcmdText,SqlParameter[]cmdParms)
    {
    if(conn.State!=ConnectionState.Open)
    conn.Open();
    cmd.Connection=conn;
    cmd.CommandText=cmdText;
    if(trans!=null)
    cmd.Transaction=trans;
    cmd.CommandType=CommandType.Text;//cmdType;
    if(cmdParms!=null)
    {
    foreach(SqlParameterparmincmdParms)
    cmd.Parameters.Add(parm);
    }
    }
    #endregion
    #region存储过程操作
    ///
    ///执行存储过程
    ///

    ///存储过程名
    ///存储过程参数
    ///SqlDataReader
    publicstaticSqlDataReaderRunProcedure(stringstoredProcName,IDataParameter[]parameters)
    {
    SqlConnectionconnection=newSqlConnection(connectionString);
    SqlDataReaderreturnReader;
    connection.Open();
    SqlCommandcommand=BuildQueryCommand(connection,storedProcName,parameters);
    command.CommandType=CommandType.StoredProcedure;
    returnReader=command.ExecuteReader();
    returnreturnReader;
    }
    ///
    ///执行存储过程自己指定数据库连接
    ///

    ///
    ///
    ///
    ///
    publicstaticSqlDataReaderRunProcedure(stringstoredProcName,IDataParameter[]parameters,SqlConnectionconnection)
    {
    SqlDataReaderreturnReader;
    //connection.Open();
    SqlCommandcommand=BuildQueryCommand(connection,storedProcName,parameters);
    command.CommandType=CommandType.StoredProcedure;
    returnReader=command.ExecuteReader();
    returnreturnReader;
    }
    ///
    ///执行存储过程
    ///

    ///存储过程名
    ///存储过程参数
    ///DataSet结果中的表名
    ///DataSet
    publicstaticDataSetRunProcedure(stringstoredProcName,IDataParameter[]parameters,stringtableName)
    {
    using(SqlConnectionconnection=newSqlConnection(connectionString))
    {
    DataSetdataSet=newDataSet();
    connection.Open();
    SqlDataAdaptersqlDA=newSqlDataAdapter();
    sqlDA.SelectCommand=BuildQueryCommand(connection,storedProcName,parameters);
    sqlDA.Fill(dataSet,tableName);
    connection.Close();
    returndataSet;
    }
    }
    #region
    #endregion
    ///
    ///构建SqlCommand对象(用来返回一个结果集,而不是一个整数值)
    ///

    ///数据库连接
    ///存储过程名
    ///存储过程参数
    ///SqlCommand
    privatestaticSqlCommandBuildQueryCommand(SqlConnectionconnection,stringstoredProcName,IDataParameter[]parameters)
    {
    SqlCommandcommand=newSqlCommand(storedProcName,connection);
    command.CommandType=CommandType.StoredProcedure;
    foreach(SqlParameterparameterinparameters)
    {
    command.Parameters.Add(parameter);
    }
    returncommand;
    }
    ///
    ///执行存储过程,返回影响的行数
    ///

    ///存储过程名
    ///存储过程参数
    ///影响的行数
    ///
    publicstaticintRunProcedure(stringstoredProcName,IDataParameter[]parameters,outintrowsAffected)
    {
    using(SqlConnectionconnection=newSqlConnection(connectionString))
    {
    intresult;
    connection.Open();
    SqlCommandcommand=BuildIntCommand(connection,storedProcName,parameters);
    rowsAffected=command.ExecuteNonQuery();
    result=(int)command.Parameters["ReturnValue"].Value;
    //Connection.Close();
    returnresult;
    }
    }
    ///
    ///创建SqlCommand对象实例(用来返回一个整数值)
    ///

    ///存储过程名
    ///存储过程参数
    ///SqlCommand对象实例
    privatestaticSqlCommandBuildIntCommand(SqlConnectionconnection,stringstoredProcName,IDataParameter[]parameters)
    {
    SqlCommandcommand=BuildQueryCommand(connection,storedProcName,parameters);
    command.Parameters.Add(newSqlParameter("ReturnValue",
    SqlDbType.Int,4,ParameterDirection.ReturnValue,
    false,0,0,string.Empty,DataRowVersion.Default,null));
    returncommand;
    }
    #endregion
    }
    }
    
  • 上一篇资讯: DbHelperSQLcs(1)
  • 下一篇资讯: DbHelperSQL(1)
  • 设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师