应用程序大多数都是需要与数据库交互做为支撑的,那么相对数据库的访问方式就相当重要了,最基础的是通过Ado的方式了;还有就是一些相对的ORM框架,如EF,Nhibernate等,后续加入; 相应的命名空间自己对应导入就可以
提供一个web.config中的连接串或app.config
<connectionStrings>
<!--Oracle 连接串-->
<add name="oracleconn" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)) (CONNECT_DATA=(SERVICE_NAME=ORCL)));User ID=scott;Password=tiger;Unicode=True" providerName="System.Data.OracleClient"/>
<!--sqlserver 连接串-->
<add name="ConstrSQL" connectionString="server=.\MSSQLSERVER1;database=数据库名;uid=sa;pwd=*****;Integrated Security=False;Persist Security Info=False;" providerName="System.Data.Sqlclient"/>
</connectionStrings>
配置连接串要放到<configSections>后边,至于那里你自己定
#region 其它几种连接方式
//static string conner = @"Data Source=.\MSSQLSERVER1;Initial Catalog=数据库名字;Persist Security Info=True;User ID=sa;pwd=***";
//static string conner = ConfigurationSettings.AppSettings["ConstrSQL"].ToString(); //ConfigurationSettings配置节点读取
#endregion
static string conner = ConfigurationManager.ConnectionStrings["ConstrSQL"].ConnectionString; //ConnectionStrings配置文件读取
private static SqlConnection con = new SqlConnection(conner);
#region 旧的
/// <summary>
/// 执行增删改操作
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int ExexuteCommand(string sql)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
/// <summary>
/// 执行增删改操作
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int ExexuteCommand(string sql, params SqlParameter[] para)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
//将参数添加到参数集合中
cmd.Parameters.AddRange(para);
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
/// <summary>
/// 返回dataReader的查询方法
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static SqlDataReader GetReader(string sql)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 返回dataReader的查询方法
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static SqlDataReader GetReader(string sql, params SqlParameter[] para)
{
try
{
//if (con.State == ConnectionState.Closed)
//{
con.Open();
//}
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddRange(para);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 返回dataTable的查询方法
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataTable GetDateTable(string sql)
{
try
{
con.Open();
SqlDataAdapter myAdapter = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
myAdapter.Fill(ds);
return ds.Tables[0];
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
/// <summary>
/// 返回dataTable的查询方法
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataTable GetDateTable(string sql, params SqlParameter[] para)
{
try
{
con.Open();
//SqlDataAdapter myAdapter = new SqlDataAdapter(sql, con);
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddRange(para);
SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
myAdapter.Fill(ds);
return ds.Tables[0];
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
/// <summary>
/// 返回单值的查询方法
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static object GetScalar(string sql)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
return cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
/// <summary>
/// 返回单值的查询方法(有参数的查询语句)
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static object GetScalar(string sql, params SqlParameter[] para)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddRange(para);
return cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
#region 存储过程调用方法
/// <summary>
/// 调用执行增删改的有参数的存储过程
/// </summary>
/// <param name="name"></param>
/// <param name="values"></param>
/// <returns></returns>
public static int ExectueCommandStoredProcedure(string name, params SqlParameter[] values)
{
//SqlConnection conn = new SqlConnection(connection);
try
{
//connection.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = con;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = name;
comm.Parameters.AddRange(values);
return comm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
/// <summary>
/// 调用无参的存储过程的方法
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int ExectueCommandStoredProcedure(string name)
{
try
{
//connection.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = con;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = name;
//comm.Parameters.AddRange(values);
return comm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
/// <summary>
/// 返回DataTable型的存储过程的调用方法
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DataTable GetTableByStoredProcedure(string name)
{
//SqlConnection conn = new SqlConnection(connection.ConnectionString);
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
DataSet ds = new DataSet();
SqlCommand comm = new SqlCommand();
comm.Connection = con;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = name;
SqlDataAdapter da = new SqlDataAdapter(comm);
ds.Clear();
da.Fill(ds);
return ds.Tables[0];
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
/// <summary>
/// 返回DataTable型的存储过程的调用方法(含参)
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DataTable GetTableByStoredProcedure(string name, params SqlParameter[] valuse)
{
//SqlConnection conn = new SqlConnection(connection.ConnectionString);
try
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
//conn.Open();
DataSet ds = new DataSet();
SqlCommand comm = new SqlCommand();
comm.Connection = con;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = name;
comm.Parameters.AddRange(valuse);
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(ds);
return ds.Tables[0];
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
/// <summary>
/// 返回reader型的无参的调用存储过程的方法
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static SqlDataReader GetReaderByStoredProcedure(string name)
{
try
{
SqlCommand comm = new SqlCommand();
comm.Connection = con;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = name;
SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 返回reader型的(含参)的调用存储过程的方法
/// </summary>
/// <param name="name"></param>
/// <param name="values"></param>
/// <returns></returns>
public static SqlDataReader GetReaderByStoredProcedure(string name, params SqlParameter[] values)
{
try
{
con.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = con;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = name;
comm.Parameters.AddRange(values);
SqlDataReader reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 返回单值类型(无参)
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static Object GetScalarByStoredProcedure(string name)
{
try
{
con.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = con;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = name;
return comm.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
/// <summary>
/// 返回单值类型(含参)
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static Object GetScalarByStoredProcedure(string name, params SqlParameter[] values)
{
try
{
con.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = con;
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = name;
comm.Parameters.AddRange(values);
return comm.ExecuteScalar();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
#endregion
#endregion
来源:https://www.cnblogs.com/gyjjyg/p/6811280.html