.NET 三层架构中放在DAL层的DBHelper
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
finally
{
if (command != null)
{
command.Connection.Close();
}
finally
{
if (command != null)
{
command.Connection.Close();
}
}
return 0;
}
//带一个参数的返回受影响行数SQL语句
public static int ExecuteNonQuery(string sql, SqlParameter sp)
return command.ExecuteNonQuery();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (command != null)
{
command.Connection.Close();
{
SqlCommand command = null;
try
{
command = CreateCommand(sql, sp);
command.Connection.Open();
}
finally
{
if (command != null)
{
command.Connection.Close();
return command.ExecuteScalar();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
return command.ExecuteNonQuery();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
command.Parameters.AddRange(sps);
return command;
}
//没有带参数的返回受影响行数SQL语句
public static int ExecuteNonQuery(string sql)
}
finally
{
if (command != null)
{
command.Connection.Close();
public static SqlCommand CreateCommand(string sql, SqlParameter[] sps)
{
SqlCommand command = new SqlCommand(sql, CreateConnection());
}
}
return 0;
}
//带一个参数的返回第一行第一列的值SQL语句
public static object ExecuteScalar(string sql, SqlParameter sp)
}
finally
{
if (command != null)
{
command.Connection.Close();
return command.ExecuteScalar();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
{
public static SqlConnection CreateConnection()
{
//return new SqlConnection(@"server=.\sqlexperss;database=jiajiaodb;user Id=sa;pwd=123456");
}
}
return 0;
}
//没有带参数的返回第一行第一列的值SQL语句
public static object ExecuteScalar(string sql)
{
SqlCommand command = null;
try
{
command = CreateCommand(sql, sp);
command.Connection.Open();
}
}
return 0;
}
//没有带参数的返回多行多列的值SQL语句
public static SqlDataReader ExecuteReader(string sql)
{
SqlCommand command=new SqlCommand(sql,CreateConnection());
command.Parameters.Add(sp);
return command;
}
return new SqlConnection("Data Source=.;Initial Catalog=Myoffice;Persist Security Info=True;User ID=sa;Password=sa");
}
public static SqlCommand CreateCommand(string sql)
}
}
return 0;
}
//带多个参数的返回受影响行数SQL语句
public static int ExecuteNonQuery(string sql, SqlParameter[] sps)
return command.ExecuteNonQuery();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
{
SqlCommand command = null;
try
{
command = CreateCommand(sql);
command.Connection.Open();
{
return new SqlCommand(sql, CreateConnection());
}
public static SqlCommand CreateCommand(string sql, SqlParameter sp)
}
}
return 0;
}
//第多个参数的返回第一行第一列的值SQL语句
public static object ExecuteScalar(string sql, SqlParameter[] sps)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace DAL
{
public class DBHelቤተ መጻሕፍቲ ባይዱer
{
SqlCommand command = null;
try
{
command = CreateCommand(sql, sps);
command.Connection.Open();
return command.ExecuteScalar();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
{
SqlCommand command = null;
try
{
command = CreateCommand(sql);
command.Connection.Open();
{
SqlCommand command = null;
try
{
command = CreateCommand(sql, sps);
command.Connection.Open();
finally
{
if (command != null)
{
command.Connection.Close();
}
finally
{
if (command != null)
{
command.Connection.Close();
}
}
return 0;
}
//带一个参数的返回受影响行数SQL语句
public static int ExecuteNonQuery(string sql, SqlParameter sp)
return command.ExecuteNonQuery();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (command != null)
{
command.Connection.Close();
{
SqlCommand command = null;
try
{
command = CreateCommand(sql, sp);
command.Connection.Open();
}
finally
{
if (command != null)
{
command.Connection.Close();
return command.ExecuteScalar();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
return command.ExecuteNonQuery();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
command.Parameters.AddRange(sps);
return command;
}
//没有带参数的返回受影响行数SQL语句
public static int ExecuteNonQuery(string sql)
}
finally
{
if (command != null)
{
command.Connection.Close();
public static SqlCommand CreateCommand(string sql, SqlParameter[] sps)
{
SqlCommand command = new SqlCommand(sql, CreateConnection());
}
}
return 0;
}
//带一个参数的返回第一行第一列的值SQL语句
public static object ExecuteScalar(string sql, SqlParameter sp)
}
finally
{
if (command != null)
{
command.Connection.Close();
return command.ExecuteScalar();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
{
public static SqlConnection CreateConnection()
{
//return new SqlConnection(@"server=.\sqlexperss;database=jiajiaodb;user Id=sa;pwd=123456");
}
}
return 0;
}
//没有带参数的返回第一行第一列的值SQL语句
public static object ExecuteScalar(string sql)
{
SqlCommand command = null;
try
{
command = CreateCommand(sql, sp);
command.Connection.Open();
}
}
return 0;
}
//没有带参数的返回多行多列的值SQL语句
public static SqlDataReader ExecuteReader(string sql)
{
SqlCommand command=new SqlCommand(sql,CreateConnection());
command.Parameters.Add(sp);
return command;
}
return new SqlConnection("Data Source=.;Initial Catalog=Myoffice;Persist Security Info=True;User ID=sa;Password=sa");
}
public static SqlCommand CreateCommand(string sql)
}
}
return 0;
}
//带多个参数的返回受影响行数SQL语句
public static int ExecuteNonQuery(string sql, SqlParameter[] sps)
return command.ExecuteNonQuery();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
{
SqlCommand command = null;
try
{
command = CreateCommand(sql);
command.Connection.Open();
{
return new SqlCommand(sql, CreateConnection());
}
public static SqlCommand CreateCommand(string sql, SqlParameter sp)
}
}
return 0;
}
//第多个参数的返回第一行第一列的值SQL语句
public static object ExecuteScalar(string sql, SqlParameter[] sps)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace DAL
{
public class DBHelቤተ መጻሕፍቲ ባይዱer
{
SqlCommand command = null;
try
{
command = CreateCommand(sql, sps);
command.Connection.Open();
return command.ExecuteScalar();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
{
SqlCommand command = null;
try
{
command = CreateCommand(sql);
command.Connection.Open();
{
SqlCommand command = null;
try
{
command = CreateCommand(sql, sps);
command.Connection.Open();