110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections;
namespace WebSite1.DataAcessLayer
{
public class Database : IDisposable
{
protected SqlConnection Connection;
protected String ConnectionString;
public Database()
{
ConnectionString = ConfigurationSettings.AppSettings["DBConnectionString"];
}
~Database()
{
try
{
if (Connection != null)
{
Connection.Close();
}
}catch { }
try
{
Dispose();
}
catch{ }
}
protected void open()
{
if(Connection == null)
{
Connection = new SqlConnection(ConnectionString);
}
if(Connection.State.Equals(ConnectionState.Closed))
{
Connection.Open();
}
}
public void Close()
{
if (Connection != null)
{
Connection.Close();
}
}
public void Dispose() //释放连接数据库所占用的资源
{
if(Connection !=null)
{
Connection.Dispose();
Connection = null;
}
}
public int ExecuteSQL(String SqlString)
{
int count = -1;
open();
try
{
SqlCommand cmd = new SqlCommand(SqlString, Connection);
count = cmd.ExecuteNonQuery();
}
catch
{
count = -1;
}
finally
{
Close();
}
return count;
}
public DataSet GetDataSet(String SqlString)
{
open();
SqlDataAdapter adapter = new SqlDataAdapter(SqlString, Connection);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
Close();
return dataset;
}
public DataRow GetDAtaRow(String SqlString)
{
DataSet dataset = GetDataSet(SqlString);
dataset.CaseSensitive = false;
if(dataset.Tables[0].Rows.Count>0)
{
return dataset.Tables[0].Rows[0];
}
else
{
return null;
}
}
}
}