加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

常用的SQLServerHelper类

发布时间:2020-12-12 15:00:32 所属栏目:MsSql教程 来源:网络整理
导读: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 strin

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;

? ? ? ? ? ? }

?

? ? ? ? }

? ? }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读