using System;
using System.Collectios.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace SqlUtility;
{
public class SqlHerper
{
public static string ConnStr=configurationManager.Connectionstrings["sql"].connectionstring;
public static int ExecuteNonQuery(string cmdTxt,params SqlPaameter[]paras)
{
if(string.IsNullOrEmpty(cmdTxt))
{
return 0;
}
using(SqlConnection conn=new SqlConnection(ConnStr))
{
using(SqlComand cmd=conn.CreateCommand())
{
cmd.CommandType=cmdTxt;
//cmd.CommandType=CommandType.StoredProcedure;
if(paras!=null)
{
cmd.Parameters.AddRange(paras);
}
conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
public static object ExecuteScalar(string cmdText,params SqlParameter[]paras)
{
if(IsNullOrEmpty)
{
return 0;
}
using(SqlConnection conn=new SqlConnection(ConnStr))
{
using (SqlCommand cmd=conn.CreateCommand())
{
if(paras!=null)
{
cmd.Parameters.AddRange(paras);
}
cmd.CommandType=cmdText;//cmd.CommandType=CommandType.StoredProcedure;
cmd.Open();
return cmd.ExecuteScalar();
}
}
}
public static DataTable ExcuteDataTable(string cmdTxt, params SqlParameter[] parames)
{
if (string.IsNullOrEmpty(cmdTxt))
{
return null;
}
DataTable dt = new DataTable();
using (SqlDataAdapter adpter = new SqlDataAdapter(cmdTxt,ConnStr))
{
if (parames != null)
{
adpter.SelectCommand.Parameters.AddRange(parames);
}
adpter.Fill(dt);
return dt;
}
}
}
}