SqlHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication1
{
public class SqlHelper
{
private static readonly string connString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
public static int ExecuteNonQuery(string sql,params SqlParameter[] parameters )
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();
}
}
}
public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteScalar();
}
}
}
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
{
DataSet set = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
if (parameters!=null)
{
cmd.Parameters.AddRange(parameters);
}
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(set);
return set.Tables[0];
}
}
}
}
}

posted @ 2017-05-16 09:55  西瓜冰镇老板  阅读(485)  评论(0编辑  收藏  举报