using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;
/// <summary>
/// Summary description for SqlHelper
/// </summary>
public class SqlHelper
{
//public static string CONN_STR = @"Server=HUDENGWEN\SQLEXPRESS;Database=MQSDB;Trusted_Connection=True";
public static string CONN_STR = System.Configuration.ConfigurationSettings.AppSettings["CONN_STRING"];
#region ExecuteNonQuery(SQL语句)
/// <summary>
/// 执行Update,delete,insert语句
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static int ExcectueNonQuery(string strSql)
{
using (SqlConnection con = new SqlConnection(CONN_STR))
{
con.Open();
SqlCommand command = new SqlCommand(strSql, con);
try
{
return command.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
}
}
#endregion
#region ExecuteDataset:返回DataSet(查询SQL语句)
/// <summary>
/// 执行查询传入Select语句 返回DataSet
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static DataSet ExecuteDataset(string strSql)
{
try
{
using (SqlConnection conn = new SqlConnection(CONN_STR))
{
conn.Open();
// System.Data.SqlServerCe.SqlCeCommand com = new SqlCeCommand(strSql, DAL.CreateConn.GetConn());
SqlDataAdapter da = new SqlDataAdapter(strSql, conn);
System.Data.DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
}
#endregion
#region ExecuteObj:传入Sql语句返回首行首列的对象(SQL语句)
/// <summary>
/// 传入Sql语句返回首行首列的对象
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static object ExecuteObj(string strSql)
{
try
{
using (SqlConnection conn = new SqlConnection(CONN_STR))
{
conn.Open();
SqlCommand command = new SqlCommand(strSql, conn);
object obj = command.ExecuteScalar();
if (object.Equals(obj, null))
{
return null;
}
else
{
return obj;
}
}
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
}
#endregion
}
浙公网安备 33010602011771号