sqlhelper
Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;
/// <summary>
/// mysqlconnect 的摘要说明
/// </summary>
public class mysqlconnect
{
private int intCount = 0;
public mysqlconnect()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection DBconn()
{
string strConn =ConfigurationManager.ConnectionStrings["CustomerDataConnectionString"].ConnectionString;
try
{
return new SqlConnection(strConn);
}
catch (Exception)
{
return null;
}
}
/// 根据查询语句从数据库检索数据
/// </summary>
/// <param name="strSelect">查询语句</param>
/// <param name="SqlConn">数据库连接</param>
/// <returns>有数据则返回DataSet对象,否则返回null</returns>
public DataSet Select(string SelectString, SqlConnection sqlConn, ref string arg_msg)
{
arg_msg = "";
SqlConnection conn;
if (sqlConn == null)
{
conn = DBconn();
}
else
{
conn = sqlConn;
}
try
{
//若数据库连接的当前状态是关闭的,则打开连接
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
SqlCommand selectCommand = new SqlCommand(SelectString, conn);
selectCommand.CommandTimeout = 1200;
selectCommand.CommandType = CommandType.Text;
mySqlDataAdapter.SelectCommand = selectCommand;
DataSet myDS = new DataSet();
mySqlDataAdapter.Fill(myDS);
return myDS;
}
catch (Exception e)
{
arg_msg = "数据检索失败:" + e.Message;
return null;
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
/// 更新数据库
/// </summary>
/// <param name="UpdateString">Update Sql语句</param>
/// <param name="SqlConn">数据库连接</param>
/// <returns>更新成功返回true</returns>
public bool Update(string UpdateString, SqlConnection SqlConn, ref string arg_msg)
{
return udiDataBase(UpdateString, SqlConn,ref arg_msg);
}
/// <summary>
/// 从数据库中删除数据
/// </summary>
/// <param name="DeleteString">Delete Sql语句</param>
/// <param name="SqlConn">数据库连接</param>
/// <returns>删除成功返回true</returns>
public bool Delete(string DeleteString, SqlConnection SqlConn, ref string arg_msg)
{
return udiDataBase(DeleteString, SqlConn, ref arg_msg);
}
/// <summary>
/// 把数据插入数据库
/// </summary>
/// <param name="InsertString">Insert Sql语句</param>
/// <param name="SqlConn">数据库连接</param>
/// <returns>插入成功返回true</returns>
public bool Insert(string InsertString, SqlConnection SqlConn, ref string arg_msg)
{
return udiDataBase(InsertString, SqlConn, ref arg_msg);
}
/// <summary>
/// 根据Sql语句更新数据库
/// </summary>
/// <param name="UDIString">更新语句</param>
/// <param name="SqlConn">数据库连接</param>
/// <returns>更新成功则返回true</returns>
public bool udiDataBase(string UDIString, SqlConnection SqlConn, ref string arg_msg)
{
arg_msg = "";
SqlConnection conn;
if (SqlConn == null)
{
conn = DBconn();
}
else
{
conn = SqlConn;
}
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
//SqlTransaction transaction;
//transaction = conn.BeginTransaction("SampleTransaction");
SqlCommand cmd = new SqlCommand(UDIString, conn);
cmd.CommandType = CommandType.Text;
// cmd.Transaction = transaction;
intCount = cmd.ExecuteNonQuery();
//transaction.Commit();
//return !(intCount < 1);
return true;
}
catch (Exception e)
{
arg_msg = "更新数据库失败:" + e.Message;
//transaction.Rollback();
return false;
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
public bool master_udiDataBase(string UDIString, ref string arg_msg)
{
arg_msg = "";
SqlConnection conn = new SqlConnection("server=127.0.0.1;database=master;uid=sa;pwd=;");
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand(UDIString, conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
return true;
}
catch (Exception e)
{
arg_msg = "更新数据库失败:" + e.Message;
return false;
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Configuration;
/// <summary>
/// mysqlconnect 的摘要说明
/// </summary>
public class mysqlconnect
{
private int intCount = 0;
public mysqlconnect()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection DBconn()
{
string strConn =ConfigurationManager.ConnectionStrings["CustomerDataConnectionString"].ConnectionString;
try
{
return new SqlConnection(strConn);
}
catch (Exception)
{
return null;
}
}
/// 根据查询语句从数据库检索数据
/// </summary>
/// <param name="strSelect">查询语句</param>
/// <param name="SqlConn">数据库连接</param>
/// <returns>有数据则返回DataSet对象,否则返回null</returns>
public DataSet Select(string SelectString, SqlConnection sqlConn, ref string arg_msg)
{
arg_msg = "";
SqlConnection conn;
if (sqlConn == null)
{
conn = DBconn();
}
else
{
conn = sqlConn;
}
try
{
//若数据库连接的当前状态是关闭的,则打开连接
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
SqlCommand selectCommand = new SqlCommand(SelectString, conn);
selectCommand.CommandTimeout = 1200;
selectCommand.CommandType = CommandType.Text;
mySqlDataAdapter.SelectCommand = selectCommand;
DataSet myDS = new DataSet();
mySqlDataAdapter.Fill(myDS);
return myDS;
}
catch (Exception e)
{
arg_msg = "数据检索失败:" + e.Message;
return null;
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
/// 更新数据库
/// </summary>
/// <param name="UpdateString">Update Sql语句</param>
/// <param name="SqlConn">数据库连接</param>
/// <returns>更新成功返回true</returns>
public bool Update(string UpdateString, SqlConnection SqlConn, ref string arg_msg)
{
return udiDataBase(UpdateString, SqlConn,ref arg_msg);
}
/// <summary>
/// 从数据库中删除数据
/// </summary>
/// <param name="DeleteString">Delete Sql语句</param>
/// <param name="SqlConn">数据库连接</param>
/// <returns>删除成功返回true</returns>
public bool Delete(string DeleteString, SqlConnection SqlConn, ref string arg_msg)
{
return udiDataBase(DeleteString, SqlConn, ref arg_msg);
}
/// <summary>
/// 把数据插入数据库
/// </summary>
/// <param name="InsertString">Insert Sql语句</param>
/// <param name="SqlConn">数据库连接</param>
/// <returns>插入成功返回true</returns>
public bool Insert(string InsertString, SqlConnection SqlConn, ref string arg_msg)
{
return udiDataBase(InsertString, SqlConn, ref arg_msg);
}
/// <summary>
/// 根据Sql语句更新数据库
/// </summary>
/// <param name="UDIString">更新语句</param>
/// <param name="SqlConn">数据库连接</param>
/// <returns>更新成功则返回true</returns>
public bool udiDataBase(string UDIString, SqlConnection SqlConn, ref string arg_msg)
{
arg_msg = "";
SqlConnection conn;
if (SqlConn == null)
{
conn = DBconn();
}
else
{
conn = SqlConn;
}
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
//SqlTransaction transaction;
//transaction = conn.BeginTransaction("SampleTransaction");
SqlCommand cmd = new SqlCommand(UDIString, conn);
cmd.CommandType = CommandType.Text;
// cmd.Transaction = transaction;
intCount = cmd.ExecuteNonQuery();
//transaction.Commit();
//return !(intCount < 1);
return true;
}
catch (Exception e)
{
arg_msg = "更新数据库失败:" + e.Message;
//transaction.Rollback();
return false;
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
public bool master_udiDataBase(string UDIString, ref string arg_msg)
{
arg_msg = "";
SqlConnection conn = new SqlConnection("server=127.0.0.1;database=master;uid=sa;pwd=;");
try
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmd = new SqlCommand(UDIString, conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
return true;
}
catch (Exception e)
{
arg_msg = "更新数据库失败:" + e.Message;
return false;
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
}