SqlHelper.cs

Posted 挥挥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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];
}
}
}
}
}

以上是关于SqlHelper.cs的主要内容,如果未能解决你的问题,请参考以下文章