47 lines
1.1 KiB
C#
47 lines
1.1 KiB
C#
|
using System;
|
|||
|
using System.Data.SqlClient;
|
|||
|
|
|||
|
namespace WebAPI.Helpers
|
|||
|
{
|
|||
|
public class DatabaseHelper : IDisposable
|
|||
|
{
|
|||
|
private SqlConnection _sqlConnection;
|
|||
|
private bool _disposed = false;
|
|||
|
|
|||
|
public DatabaseHelper(string connectionString)
|
|||
|
{
|
|||
|
_sqlConnection = new SqlConnection(connectionString);
|
|||
|
_sqlConnection.Open();
|
|||
|
}
|
|||
|
|
|||
|
public SqlConnection GetConnection()
|
|||
|
{
|
|||
|
return _sqlConnection;
|
|||
|
}
|
|||
|
|
|||
|
public void Dispose()
|
|||
|
{
|
|||
|
Dispose(true);
|
|||
|
GC.SuppressFinalize(this);
|
|||
|
}
|
|||
|
|
|||
|
protected virtual void Dispose(bool disposing)
|
|||
|
{
|
|||
|
if (!_disposed)
|
|||
|
{
|
|||
|
if (disposing)
|
|||
|
{
|
|||
|
// 释放托管资源
|
|||
|
if (_sqlConnection != null)
|
|||
|
{
|
|||
|
_sqlConnection.Close();
|
|||
|
_sqlConnection.Dispose();
|
|||
|
_sqlConnection = null;
|
|||
|
}
|
|||
|
}
|
|||
|
// 释放非托管资源
|
|||
|
_disposed = true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|