view plaincopy to clipboardprint?
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;
/// <summary>
/// 数据库基本操作类
/// </summary>
public class SQLHelper
{
//数据库连接字符串
private string connSTR;
//数据库连接对象
private SqlConnection myConnection;
//数据库操作命令对象
private SqlCommand myCommand;
/// <summary>
/// 构造函数,创建连接
/// </summary>
public SQLHelper()
{
connSTR = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
myConnection = new SqlConnection(connSTR);
}
#region 执行带参数的sql语句(插入、删除、修改),返回-1表示执行失败
public int ExcuSqlWithPara(string cmdText, SqlParameter[] para)
{
//创建Command
myCommand = new SqlCommand(cmdText, myConnection);
//传递参数
for (int i = 0; i < para.Length; i++)
{
myCommand.Parameters.Add(para[i]);
}
//定义返回值
int nResult = -1;
try
{
//打开链接
myConnection.Open();
//执行SQL语句
nResult = myCommand.ExecuteNonQuery();
}
catch (SqlException ex)
{
//抛出异常
throw new Exception(ex.Message, ex);
}
finally
{ //关闭链接
myConnection.Close();
}
//返回nResult
return nResult;
}
#endregion
}