using System.Data.SqlClient;
using System.Windows.Forms;
namespace Sql
{
class DbManager
{
private SqlConnection conn;
public SqlConnection Conn
{
get
{
if (this.conn == null)
{
conn = new SqlConnection(); // Eğer Boşsa Bağlantı Nesnesini Oluştur
conn.ConnectionString = GetConnectionString();
}
if (conn.State != System.Data.ConnectionState.Open)
{
try
{
conn.Open(); //Bağlantı Açık Değil is Aç
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, "Hata : Sql Bağlantısı Açılamadı");
}
}
return conn;
}
}
public void BaglantiyiKapat()
{
if (Conn.State == System.Data.ConnectionState.Open)
{
try
{
Conn.Close(); //Eğer Bağlantı Açık ise Kapat
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message, "Hata : SQL Bağlantısı Kapatılamadı");
}
}
}
string GetConnectionString() //SQL Connection String Oluşturucu
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "SQLSERVER"; //SQL Sunucu Adı
builder.UserID = "sa"; //Kullanıcı Adı
builder.Password = "1234"; //Kullanıcı Şifresi
builder.InitialCatalog = "DATABASE"; //Veri Tabanı
return builder.ToString();
}
}
}
Son Yorumlar