using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;
namespace Qa.DAL
{
public static class DBHelper
{
private static MySqlConnection connection;
public static MySqlConnection Connection
{
get
{
string connectionString = ConfigurationManager.ConnectionStrings["MyQa"].ToString();
if (connection == null)
{
connection = new MySqlConnection(connectionString);
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();
}
return connection;
}
}
public static MySqlConnection MyConn = new MySqlConnection("Server=localhost;userid=root;password=root;Database=zzz");
public static int ExecuteCommand(string safeSql)
{
MySqlCommand cmd = new MySqlCommand(safeSql, Connection);
int result = cmd.ExecuteNonQuery();
return result;
}
public static int ExecuteCommand(string sql, params MySqlParameter[] values)
{
MySqlCommand cmd = new MySqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery();
}
public static int GetScalar(string safeSql)
{
MySqlCommand cmd = new MySqlCommand(safeSql, Connection);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
public static int GetScalar(params SqlParameter[] values)
{
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = Connection;
cmd.CommandText = "Pro_InsertOrder";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
public static int GetScalar(string sql, params SqlParameter[] values)
{
MySqlCommand cmd = new MySqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
int result = Convert.ToInt32(cmd.ExecuteScalar());
return result;
}
public static MySqlDataReader GetReader(string safeSql)
{
MySqlCommand cmd = new MySqlCommand(safeSql, Connection);
MySqlDataReader reader = cmd.ExecuteReader();
return reader;
}
public static MySqlDataReader GetReader(string sql, params MySqlParameter[] values)
{
MySqlCommand cmd = new MySqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
MySqlDataReader reader = cmd.ExecuteReader();
return reader;
}
public static DataTable GetDataSet(string safeSql)
{
DataSet ds = new DataSet();
MySqlCommand cmd = new MySqlCommand(safeSql, Connection);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
public static DataTable GetDataSet(string sql, params MySqlParameter[] values)
{
DataSet ds = new DataSet();
MySqlCommand cmd = new MySqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0];
}
}
}