using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace GetRoomPrice
{
public class DBHelper
{
/// <summary>
/// 连接字符串
/// </summary>
public static readonly string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
/// <summary>
/// 执行SQL语句的增删改方法
/// </summary>
/// <param name="sqlText">SQL语句</param>
/// <returns></returns>
public static int ExecuteNonQuery(string sqlText, SqlParameter[] param)
{
int result = 0;
using (SqlConnection con = new SqlConnection(ConString))
{
SqlCommand cmd = new SqlCommand(sqlText, con);
if (param != null)
cmd.Parameters.AddRange(param);
con.Open();
result = cmd.ExecuteNonQuery();
}
return result;
}
public static DataTable ExecuteReader(string sqlText, SqlParameter[] param)
{
DataTable table = new DataTable();
using (SqlConnection con = new SqlConnection(ConString))
{
SqlCommand cmd = new SqlCommand(sqlText, con);
if (param != null)
cmd.Parameters.AddRange(param);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(table);
}
return table;
}
public static int GetIdentity(string sql, SqlParameter[] param)
{
object obj = null;
using (SqlConnection con = new SqlConnection(ConString))
{
SqlCommand cmd = new SqlCommand(sql, con);
if (param != null)
cmd.Parameters.AddRange(param);
con.Open();
obj = cmd.ExecuteScalar();
}
return obj == null ? -1 : Convert.ToInt32(obj);
}
public static SqlCommand CreateCommand(string commandText, SqlConnection con)
{
if (con.State != ConnectionState.Open)
con.Open();
return new SqlCommand(commandText, con);
}
/// <summary>
/// 查询返回单个值
/// </summary>
/// <param name="commandText"></param>
/// <returns></returns>
public static object ExecuteScalar(string commandText)
{
using (SqlConnection con = new SqlConnection(ConString))
{
SqlCommand cmd = CreateCommand(commandText, con);
return cmd.ExecuteScalar();
}
}
}
}