Skip to content

Instantly share code, notes, and snippets.

@erkantaylan
Last active January 3, 2018 08:48
Show Gist options
  • Select an option

  • Save erkantaylan/c9f5184f5ff492850a9e92780c44b406 to your computer and use it in GitHub Desktop.

Select an option

Save erkantaylan/c9f5184f5ff492850a9e92780c44b406 to your computer and use it in GitHub Desktop.
using System;
using System.Data;
using System.Data.SqlClient;
internal class DbConnection
{
SqlConnection _sqlCnn; //Bağlantı
SqlCommand _sqlCmd; //Sql Komutu
SqlDataAdapter _sqlDa;
private readonly string _cnnStr; //= "Data Source=localhost;Initial Catalog=bilmuh;Integrated Security=True";
public DbConnection(string cnnStrGonderilen) //Eğer bağlantı stringi farklılık gösterirse
{
_cnnStr = cnnStrGonderilen;
}
public DbConnection()//Default bağlantı stringi
{
_cnnStr = "Data Source=CEH\\DATA;Initial Catalog=OgrenciHesapDefteri;Integrated Security=True";
}
public int RunCommand(string cmdStr) // Tüm ekleme ve guncelleme komutları string olarak gönderilecek çalıştırıldıysa true dondurulecek.
{
int sonuc;
_sqlCnn = new SqlConnection(_cnnStr);
_sqlCmd = new SqlCommand(cmdStr, _sqlCnn);
try
{
_sqlCnn.Open();
sonuc = _sqlCmd.ExecuteNonQuery();
_sqlCnn.Close();
}
catch
{
sonuc = -1;
_sqlCnn.Close();
}
return sonuc;
}
public DataTable SelectTable(string cmdStr)// Table donduruyo mesela -Select * from Tabloadı- gonderildiğinde direk sorgu sonucunu dönduruyo tablo olarak
{
_sqlCnn = new SqlConnection(_cnnStr);
_sqlCmd = new SqlCommand(cmdStr, _sqlCnn);
_sqlDa = new SqlDataAdapter(_sqlCmd);
DataTable dt = new DataTable();
try
{
_sqlCnn.Open();
_sqlDa.Fill(dt);
//sqlCnn.Close();
}
catch (Exception e)
{
_sqlCnn.Close();
throw new Exception(e.ToString());
}
return dt;
}
public void ConnectionClose()
{
_sqlCnn.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment