net Implements a Simple General Query Data and Exports Excel Web Page
Posted by hbradshaw on Fri, 04 Oct 2019 12:13:02 +0200
Background:
Provide a simple web page temporarily for others to browse some data in the database (Oracel, MSSQL) and export Excel.
Support adding or modifying sql at any time in the configuration file.
Realization:
Store sql statements and other information in an xml file. The front-end page provides a drop-down box to select different types of sql statements. The query results (not required for paging) are directly bound to GridView.
Development environment: VS2015
NuGet introduces libraries: NPOI, Oracle.ManagedDataAccess.Client
I. VS Directory
├─App_Code
DataBase.cs------------------- Database Connection Class ____________
Excel Helper. CS - -------- Export Excel Tool Class
SqlEntity.cs--------------- Entity class of SQL statement
SqlEntityList.cs - --------------------------------------------------------------------------------------------------------------------------------------
│
├─App_Data
Sql. XML - - - - - - SQL statement
│
rpt.aspx - - - - - - Front-end query page
│ rpt.aspx.cs ---------
Web.config - - - - - - Configuring database connection information
Two, code
1,Web.config
<connectionStrings>
<add name="OracleString" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=testdb)));Persist Security Info=True;User ID=dev;Password=123456" providerName="Oracle.ManagedDataAccess.Client"/>
<add name="SqlServerString" connectionString="user id=sa; password=123456; database=test; server=localhost" providerName="System.Data.SqlClient"/>
</connectionStrings>
Database connection string
2,DataBase.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
/// <summary>
/// DataBase Summary description of
/// </summary>
public class DataBase
{
private DbConnection cnn;//Abstract type
private DbCommand cmd;//Abstract type
private DbProviderFactory provider;
private string providerName;
public DataBase(String connectionName)
{
providerName = WebConfigurationManager.ConnectionStrings[connectionName].ProviderName;
provider = DbProviderFactories.GetFactory(providerName);
cnn = provider.CreateConnection();
cnn.ConnectionString = WebConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
cmd = provider.CreateCommand();
cmd.Connection = cnn;
}
#region Executing parameterized SQL Sentence
/// <summary>
/// implement SQL Statement, returning the number of records affected
/// </summary>
public int ExecuteSQL(string sql)
{
return ExecuteSQL(sql, null);
}
/// <summary>
/// Execute multiple SQL Statement to achieve database transactions.
/// </summary>
public int ExecuteSqlTran(List<string> sqlList)
{
int count = -1;
cnn.Open();
DbTransaction tx = cnn.BeginTransaction();
try
{
cmd.Transaction = tx;
for (int n = 0; n < sqlList.Count; n++)
{
string strsql = sqlList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
count = cmd.ExecuteNonQuery();
}
}
tx.Commit();
}
catch (SqlException e)
{
tx.Rollback();
cnn.Close();
throw new Exception(e.Message);
}
return count;
}
/// <summary>
/// Execute a query result statement and return the query result( object).
/// </summary>
public int ExecuteScalar(string sql)
{
return ExecuteScalar(sql, null);
}
/// <summary>
/// Execute the query statement and return DataSet
/// </summary>
public DataSet GetDataSet(string sql)
{
return GetDataSet(sql, null);
}
/// <summary>
/// Execute the query statement and return DataSet
/// </summary>
public DataTable GetDataTable(string sql)
{
return GetDataSet(sql).Tables[0];
}
/// <summary>
/// Execute the query statement and return DataReader(Use this method to remember to close manually DataReader And connection)
/// </summary>
public DbDataReader ExecuteReader(string sql)
{
return ExecuteReader(sql, null);
}
#endregion
#region Executing parameterized SQL Sentence
/// <summary>
/// implement SQL Statement, returning the number of records affected
/// </summary>
public int ExecuteSQL(string sql, params DbParameter[] cmdParms)
{
try
{
CreateCommand(sql, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (SqlException e)
{
cnn.Close();
throw new Exception(e.Message);
}
}
/// <summary>
/// Execute multiple SQL Statement to achieve database transactions.
/// </summary>
public int ExecuteSqlTran(Hashtable sqlList)
{
int count = -1;
cnn.Open();
DbTransaction tx = cnn.BeginTransaction();
try
{
cmd.Transaction = tx;
foreach (DictionaryEntry myDE in sqlList)
{
string cmdText = myDE.Key.ToString();
DbParameter[] cmdParms = (DbParameter[])myDE.Value;
CreateCommand(cmdText, cmdParms);
count = cmd.ExecuteNonQuery();
}
tx.Commit();
}
catch (SqlException e)
{
tx.Rollback();
cnn.Close();
throw new Exception(e.Message);
}
return count;
}
/// <summary>
/// Execute a query result statement and return the query result( object).
/// </summary>
public int ExecuteScalar(string sql, params DbParameter[] cmdParms)
{
try
{
CreateCommand(sql, cmdParms);
object o = cmd.ExecuteScalar();
return int.Parse(o.ToString());
}
catch (SqlException e)
{
cnn.Close();
throw new Exception(e.Message);
}
}
/// <summary>
/// Execute the query statement and return DataSet
/// </summary>
public DataSet GetDataSet(string sql, params DbParameter[] cmdParms)
{
DataSet ds = new DataSet();
try
{
CreateCommand(sql, cmdParms);
DbDataAdapter adapter = provider.CreateDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
}
catch (SqlException e)
{
cnn.Close();
throw new Exception(e.Message);
}
return ds;
}
/// <summary>
/// Execute the query statement and return DataTable
/// </summary>
public DataTable GetDataTable(string sql, params DbParameter[] cmdParms)
{
return GetDataSet(sql, cmdParms).Tables[0];
}
/// <summary>
/// Execute the query statement and return DataReader(Use this method to remember to close manually DataReader And connection)
/// </summary>
public DbDataReader ExecuteReader(string sql, params DbParameter[] cmdParms)
{
try
{
CreateCommand(sql, cmdParms);
DbDataReader myReader = cmd.ExecuteReader();
return myReader;
}
catch (SqlException e)
{
cnn.Close();
throw new Exception(e.Message);
}
}
public DbParameter MakeParam(string ParamName, DbType DbType, Int32 Size, object Value)
{
DbParameter Param = cmd.CreateParameter();
Param.ParameterName = ParamName;
Param.DbType = DbType;
if (Size > 0)
Param.Size = Size;
if (Value != null)
Param.Value = Value;
return Param;
}
private DbCommand CreateCommand(string cmdText, DbParameter[] Prams)
{
return CreateCommand(CommandType.Text, cmdText, Prams);
}
private DbCommand CreateCommand(CommandType cmdType, string cmdText, DbParameter[] Prams)
{
if (cnn.State != ConnectionState.Open)
cnn.Open();
cmd.CommandType = cmdType;
cmd.CommandText = cmdText;
if (Prams != null)
{
cmd.Parameters.Clear();
foreach (DbParameter Parameter in Prams)
cmd.Parameters.Add(Parameter);
}
return cmd;
}
public DataSet GetDataSetByProc(string ProcName, DbParameter[] Params)
{
cnn.Open();
DbCommand cmd = CreateCommand(CommandType.StoredProcedure, ProcName, Params);
DbDataAdapter adapter = provider.CreateDataAdapter();
adapter.SelectCommand = cmd;
DataSet ds = new DataSet();
adapter.Fill(ds);
cnn.Close();
return ds;
}
#endregion
}
Database Connection Class