1 回答
TA贡献1860条经验 获得超8个赞
你看下 这个对不
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace SQL.MySchoolDAL
{
public static class DBHelper
{
private static string connectionString =
ConfigurationManager.ConnectionStrings["conString"].ToString();
private static SqlConnection connection;
public static SqlConnection Connection
{
get
{
if (connection == null || connection.ConnectionString=="")
{
connection = new SqlConnection(connectionString);
connection.Open();
}
else if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
public static int ExecuteCommand(string sql, CommandType commandType, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
cmd.CommandType = commandType;
int result = cmd.ExecuteNonQuery();
return result;
}
public static int GetScalar(string sql, CommandType commandType, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
cmd.CommandType = commandType;
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
public static SqlDataReader GetReader(string sql, CommandType commandType, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
cmd.CommandType = commandType;
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
public static DataSet GetDataSet(string sql, CommandType commandType, params SqlParameter[] values)
{
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, Connection);
da.SelectCommand.Parameters.AddRange(values);
da.SelectCommand.CommandType = commandType;
da.Fill(ds);
return ds;
}
}
}
- 1 回答
- 0 关注
- 823 浏览
添加回答
举报