using System.Data;
using System.Data.SqlClient;
namespace SQLServer
{
public class SQLServerDataBase
{
string _connString = "server=127.0.0.1;database=User;uid=sa;pwd=123";
public SQLServerDataBase(string connStr)
{
_connString = connStr;
}
public bool OpenDataBase()
{
try
{
//创建数据库连接对象
using (SqlConnection sqlConn = new SqlConnection(_connString))
{
//打开连接
sqlConn.Open();
sqlConn.Close();
return true;
}
}
catch
{
return false;
}
}
///<summary>
///定义函数根据传入的参数,执行SQL语句获取影响的行数
///</summary>
public bool ExecuteNonQuery(string sql, out int iResult)
{
iResult = 0;
//创建连接数据库对象
using (SqlConnection sqlConn = new SqlConnection(_connString))
{
//打开连接
sqlConn.Open();
//创建执行SQL语句对象
using (SqlCommand sqlCmd = new SqlCommand(sql, sqlConn))
{
iResult = sqlCmd.ExecuteNonQuery();
sqlConn.Close();
sqlCmd.Dispose();
return true;
}
}
}
///<summary>
///定义函数根据传入的参数,执行SQL语句获取数据集DataSet对象
///</summary>
public bool ExecuteNonDataSet(string sql, out DataSet dataSet)
{
dataSet = null;
//创建连接数据库对象
using (SqlConnection sqlConn = new SqlConnection(_connString))
{
//创建执行SQL语句对象
using (SqlDataAdapter sqlData = new SqlDataAdapter(sql, sqlConn))
{
//打开连接
sqlConn.Open();
dataSet = new DataSet();
sqlData.Fill(dataSet, "ds");
sqlConn.Close();
sqlData.Dispose();
return true;
}
}
}
}
}