using System;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace AccesTableConnect
{
class ConnectSetting
{
#region "Definitions"
public OleDbConnection DbConnect = new OleDbConnection();
private OleDbConnectionStringBuilder DbConnectString = new OleDbConnectionStringBuilder();
private string this_Folder;
private const string myDatabaseName = "Database.accdb";
private const string myDatabaseFolderName = @"\DB";
#endregion
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
if (DbConnect != null)
{
DbConnect.Dispose();
DbConnect = null;
}
}
~ConnectSet()
{
Dispose(false);
}
public OleDbConnection DbConnection()
{
try
{
DbConnectString.Provider = "Microsoft.ACE.OLEDB.12.0";
DbConnectString.DataSource = File_Name(myDatabaseName);
DbConnectString.PersistSecurityInfo = false;
DbConnect.ConnectionString = DbConnectString.ToString();
if (!(DbConnect.State == ConnectionState.Open))
{
if (DbConnect != null)
{
if (!(DbConnect.State == ConnectionState.Closed))
{
DbConnect = null;
DbConnect.Close();
}
}
DbConnect.Open();
}
return DbConnect;
}
catch (OleDbException OleDbExp)
{
MessageBox.Show("Belirtilen veritabanına bağlanamıyor", "Hata Kodu : " + OleDbExp.ErrorCode.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
private string Get_DatabaseFolder(string database_FolderName)
{
this_Folder = Directory.GetCurrentDirectory();
try
{
for (int iCount = 0; iCount <= 6 - 1; iCount++)
{
if (Directory.Exists((this_Folder + database_FolderName)))
{
return Path.GetFullPath((string.Format(@"{0}{1}\", this_Folder, database_FolderName)));
}
this_Folder = (this_Folder + @"..\");
}
}
catch (FileNotFoundException FileNotExp)
{
MessageBox.Show("Veritabanı tanımlanan klasör içerisinde yer almıyor. Lütfen Kontrol edin.", "Hata:" + FileNotExp.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return this_Folder;
}
public string File_Name(string string_FileName)
{
return Get_DatabaseFolder(myDatabaseFolderName) + string_FileName;
}
public void DisConnect()
{
if (DbConnect != null && DbConnect.State == ConnectionState.Open)
DbConnect.Close();
if (DbConnect != null)
DbConnect.Dispose();
DbConnect = null;
}
}
}