using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace MyOffice.DAL
{
public static class DBHelper
{
static string connectionString = ConfigurationManager.ConnectionStrings["stz"].ConnectionString ;
public static int ExecuteCommand(string sql, params SqlParameter[] values)
{
SqlConnection Connection = new SqlConnection(connectionString);
Connection.Open();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
int result = cmd.ExecuteNonQuery();
Connection.Close();
return result;
}
//这里是为了,用ComdSmith生成时用的Id是string 类型的,所以写这个方法
public static string GetScalar(string sql,int count,params SqlParameter[] values)
{
SqlConnection Connection = new SqlConnection(connectionString);
Connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = Connection;
cmd.CommandText = "Pro_InsertOrder";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
string result = cmd.ExecuteScalar().ToString();
Connection.Close();
return result;
}
public static int GetScalar(string sql, params SqlParameter[] values)
{
SqlConnection Connection = new SqlConnection(connectionString);
Connection.Open();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
Connection.Close();
return result;
}
public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
SqlConnection Connection = new SqlConnection(connectionString);
Connection.Open();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
public static DataTable GetDataSet(string sql, params SqlParameter[] values)
{
SqlConnection Connection = new SqlConnection(connectionString);
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
}
}