C# asp.net 数据库连接wj-wangjun
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
namespace DAL
{
public class DBAccess
{
private SqlConnection objConn;
private SqlCommand objComm;
public DBAccess(string strConn)
{
objConn = new SqlConnection(strConn);
}
//增,删,改
public bool ExecuteSQL(string strSQL,out string strMessage)
{
try
{
objConn.Open();
objComm = new SqlCommand(strSQL, objConn);
strMessage = objComm.ExecuteNonQuery().ToString();
return true;
}
catch (Exception ex)
{
strMessage = ex.Message;
return false;
}
finally
{
if(objConn!=null)
{
objConn.Close();
}
}
}
//查
public bool SearchSQL(string strSelSQL, out DataSet dsResult, out string strMessage)
{
DataSet objDataSet = new DataSet();
try
{
objConn.Open();
SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(strSelSQL,objConn);
objSqlDataAdapter.Fill(objDataSet);
strMessage = "";
return true;
}
catch (Exception ex)
{
strMessage = ex.Message;
return false;
}
finally
{
dsResult = objDataSet;
if(objConn!=null)
{
objConn.Close();
}
}
}
}
}