问题描述
usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Data.Sql;usingSystem.Data.SqlClient;///<summary>///DB的摘要说明///</summary>publicclassDB{#region公共成员publicstaticSqlConnectionsqlconnection;//定义公共成员publicstaticreadonlystringcnstr="server=fuck\myds;database=zd_national;UserId=sa;pwd=123456;pooling=false";//数据库连接字符串publicstaticreadonlystringcnstr2="server=fuck\myds;database=master;UserId=sa;pwd=123456;pooling=false";//数据库连接字符串#endregionpublicDB(){////TODO:在此处添加构造函数逻辑//}#region打开数据库连接///<summary>///打开数据库连接返回SqlConnection///</summary>///<returns></returns>publicstaticSqlConnectionOpenConnection(){try{sqlconnection=newSqlConnection(cnstr);sqlconnection.Open();returnsqlconnection;}catch(Exceptionex){thrownewException(ex.Message);}}#endregion#region关闭数据库连接并释放资源///<summary>///关闭数据库连接释放资源///</summary>///<paramname="Conn">数据库连接对象</param>publicstaticvoidDisposeConnection(SqlConnectionConn){if(Conn!=null){Conn.Close();Conn.Dispose();}}#endregion#region执行Sql查询语句///<summary>///执行Sql查询语句执行成功返回受影响的行数,执行失败返回-1;///</summary>///<paramname="strSQL">传入的Sql语句</param>///<returns>返回受影响的行数</returns>publicstaticintExecuteSql(stringstrSQL){SqlConnectionconn=OpenConnection();try{SqlCommandcomm=newSqlCommand(strSQL,conn);intval=comm.ExecuteNonQuery();DisposeConnection(conn);returnval;}catch(Exceptione){DisposeConnection(conn);thrownewException(e.Message);}}#endregion#region返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接///<summary>///返回指定Sql语句的SqlDataReader,请注意,在使用后请关闭本对象,同时将自动调用closeConnection()来关闭数据库连接///</summary>///<paramname="strSQL">传入的Sql语句</param>///<returns>SqlDataReader对象</returns>publicstaticSqlDataReadergetDataReader(stringstrSQL,SqlConnectioncn){SqlDataReaderdr=null;try{SqlCommandcomm=newSqlCommand(strSQL,cn);dr=comm.ExecuteReader();returndr;}catch(Exceptionex){if(dr!=null&&!dr.IsClosed)dr.Close();DisposeConnection(cn);thrownewException(ex.Message);}}#endregion#region返回指定Sql语句的DataTable///<summary>///返回指定Sql语句的DataTable///</summary>///<paramname="strSQL">传入的Sql语句</param>///<returns>DataTable</returns>publicstaticDataTablegetDataTable(stringstrSQL){SqlConnectionconn=OpenConnection();try{SqlCommandcomm=newSqlCommand(strSQL,conn);SqlDataAdapterda=newSqlDataAdapter(comm);DataTabletable=newDataTable();da.Fill(table);DisposeConnection(conn);returntable;}catch(Exceptionex){DisposeConnection(conn);thrownewException(ex.Message);}}#endregion#region返回指定Sql语句的DataSet///<summary>///返回指定Sql语句的DataSet///</summary>///<paramname="strSQL">传入的Sql语句</param>///<returns>DataSet</returns>publicstaticDataSetgetDataSet(stringstrSQL){DataSetds=newDataSet();SqlDataAdapterda=newSqlDataAdapter();SqlConnectionconn=OpenConnection();try{SqlCommandcomm=newSqlCommand(strSQL,conn);comm.CommandType=CommandType.Text;da.SelectCommand=comm;da.Fill(ds);DisposeConnection(conn);returnds;}catch(Exceptione){DisposeConnection(conn);thrownewException(e.Message);}}#endregion#region查询数据库中是否存在该条数据存在返回True,不存在返回False///<summary>///是否存在值///</summary>///<paramname="strSQL"></param>///<returns></returns>publicstaticboolisExists(stringstrSQL){SqlConnectionconn=OpenConnection();try{SqlCommandcomm=newSqlCommand(strSQL,conn);SqlDataReaderdr=comm.ExecuteReader();if(dr.HasRows)returntrue;DisposeConnection(conn);returnfalse;}catch(Exceptionex){DisposeConnection(conn);thrownewException(ex.Message);}}#endregion#region获取记录总数///<summary>///获取查询记录总数///</summary>///<paramname="strSql"></param>///<returns></returns>publicstaticintgetRowCount(stringtableNm){SqlConnectioncn=OpenConnection();intintRowCount=0;stringstr="selectcount(*)from("+tableNm+")";SqlCommandcmd=newSqlCommand(str,cn);intRowCount=(int)cmd.ExecuteScalar();DisposeConnection(cn);returnintRowCount;}#endregion#region为DropDownList绑定数据publicstaticvoidBind_Dropdownlist(stringsql,DropDownListddl,stringvalue,stringtextvalue){ddl.DataSource=getDataTable(sql);ddl.DataTextField=textvalue;ddl.DataValueField=value;ddl.DataBind();}#endregion#region为Repeater绑定数据publicstaticvoidBind_Repeater(stringsql,Repeaterrpt,SqlConnectioncn){SqlDataReaderdr=getDataReader(sql,cn);rpt.DataSource=dr;rpt.DataBind();dr.Close();dr.Dispose();}#endregion}
解决方案
解决方案二:
基本不用改,下载对应的oracle的dll,引用,把你的sqlxxxx全部修改为oraclexxx就可以了。
解决方案三:
唉,你就找下Dapper吧,不用那么麻烦
解决方案四:
这么长。。。。。。。。。。。。。。。。。。。。。。。