一个项目中的DBHelp实现
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace MyBookShop.DAL
{
public static class DBHelper
{
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
string connectionString = "Data Source=localhost;Initial Catalog=MyBookShop; uid=sa;Password=123456";
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
public static int ExecuteCommand(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
int result = cmd.ExecuteNonQuery();
return result;
}
public static int ExecuteCommand(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
public static int GetScalar(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
public static int GetScalar(params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
cmd.CommandText = "Pro_InsertOrder";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
public static int GetScalar(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
public static SqlDataReader GetReader(string safeSql)
{
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
}
public static DataTable GetDataSet(string safeSql)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(safeSql, Connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
public static DataTable GetDataSet(string sql, params SqlParameter[] values)
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
}
}
1 模块层
2
3 数据访问层(所有对数据的增 删 改 查)
4 Create
5 boolAddUser(User u)
6 Select
7 IList<User>GetAllUsers()
8 IList<User>GetUsersBySql(String sql)
9 Use GetUserById(int id)
10 Update
11 bool UpdateUser(User user)
12 Delete
13 bool DeleteUserById(int id)
14
15 BLL
16 UI
17
18 SqlConnection;
19 SqlCommand;
20 SqlDataReader;
21 SqlParameter;
22
23 DataSet;
24 IDataAdapter;
25 DataTable;
26 DataColumn;
27 DataRow;
28 DataView;
2
3 数据访问层(所有对数据的增 删 改 查)
4 Create
5 boolAddUser(User u)
6 Select
7 IList<User>GetAllUsers()
8 IList<User>GetUsersBySql(String sql)
9 Use GetUserById(int id)
10 Update
11 bool UpdateUser(User user)
12 Delete
13 bool DeleteUserById(int id)
14
15 BLL
16 UI
17
18 SqlConnection;
19 SqlCommand;
20 SqlDataReader;
21 SqlParameter;
22
23 DataSet;
24 IDataAdapter;
25 DataTable;
26 DataColumn;
27 DataRow;
28 DataView;