常用的SQLServerHelper类
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Configuration; ? namespace FlashWeb.DAL { ? ? public class DbHelper ? ? { ? ? ? ? private static readonly string CONNECTION_STRING = ? ? ? ? ? ? ConfigurationManager.ConnectionStrings["FlashConnectionString"].ConnectionString; ? ? ? ? public static int ExecuteNonQuery(string sql,params SqlParameter[] parameters) ? ? ? ? { ? ? ? ? ? ? using (SqlConnection connection = new SqlConnection(CONNECTION_STRING)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? SqlCommand sqlCommand = new SqlCommand(sql,connection); ? ? ? ? ? ? ? ? if(parameters!=null) ? ? ? ? ? ? ? ? sqlCommand.Parameters.AddRange(parameters); ? ? ? ? ? ? ? ? ? connection.Open(); ? ? ? ? ? ? ? ? ? return sqlCommand.ExecuteNonQuery(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? public static SqlDataReader ExecuteDataReader(string sql,params SqlParameter[] parameters) ? ? ? ? { ? ? ? ? ? ? SqlConnection connection = new SqlConnection(CONNECTION_STRING); ? ? ? ? ? ? SqlCommand sqlCommand = new SqlCommand(sql,connection); ? ? ? ? ? ? if (parameters != null) ? ? ? ? ? ? sqlCommand.Parameters.AddRange(parameters); ? ? ? ? ? ? ? connection.Open(); ? ? ? ? ? ? ? return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection); ? ? ? ? } ? ? ? ? ? public static object ExecuteScalar(string sql,connection); ? ? ? ? ? ? ? ? if (parameters != null) ? ? ? ? ? ? ? ? sqlCommand.Parameters.AddRange(parameters); ? ? ? ? ? ? ? ? ? connection.Open(); ? ? ? ? ? ? ? ? ? return sqlCommand.ExecuteScalar(); ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? public static DataSet ExecuteDataSet(string sqlText,CommandType commandType,params SqlParameter[] param) ? ? ? ? { ? ? ? ? ? ? using (SqlConnection connection=new SqlConnection(CONNECTION_STRING)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? SqlCommand command = new SqlCommand(sqlText,connection); ? ? ? ? ? ? ? ? if (commandType != null) ? ? ? ? ? ? ? ? ? ? command.CommandType = commandType; ? ? ? ? ? ? ? ? if (param != null) ? ? ? ? ? ? ? ? command.Parameters.AddRange(param); ? ? ? ? ? ? ? ? SqlDataAdapter da = new SqlDataAdapter(command); ? ? ? ? ? ? ? ? DataSet ds = new DataSet(); ? ? ? ? ? ? ? ? da.Fill(ds); ? ? ? ? ? ? ? ? command.Parameters.Clear(); ? ? ? ? ? ? ? ? return ds; ? ? ? ? ? ? } ? ? ? ? ? } ? ? } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |