304 lines
10 KiB
C#
304 lines
10 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Data;
|
|
|
|
namespace Payroll.BO
|
|
{
|
|
public enum ConnectionType
|
|
{
|
|
SQL = 1,
|
|
Oracle = 2,
|
|
Oledb
|
|
}
|
|
public class Connection
|
|
{
|
|
public Connection() { }
|
|
|
|
private string _Key = "";
|
|
public string Key
|
|
{
|
|
get
|
|
{
|
|
return _DataSource + "-" + _DataBase + "-" + _UserID; ;
|
|
}
|
|
set
|
|
{
|
|
_Key = value;
|
|
}
|
|
}
|
|
|
|
private string _DataSource = "";
|
|
public string DataSource
|
|
{
|
|
get { return _DataSource; }
|
|
set { _DataSource = value; }
|
|
}
|
|
|
|
private string _DataBase = "";
|
|
public string DataBase
|
|
{
|
|
get { return _DataBase; }
|
|
set { _DataBase = value; }
|
|
}
|
|
|
|
private string _UserID = "";
|
|
public string UserID
|
|
{
|
|
get { return _UserID; }
|
|
set { _UserID = value; }
|
|
}
|
|
|
|
private string _Password = "";
|
|
public string Password
|
|
{
|
|
get { return _Password; }
|
|
set { _Password = value; }
|
|
}
|
|
|
|
private string _Provider = "";
|
|
public string Provider
|
|
{
|
|
get { return _Provider; }
|
|
set { _Provider = value; }
|
|
}
|
|
|
|
private ConnectionType _ConnectionType = ConnectionType.SQL;
|
|
public ConnectionType ConnectionType
|
|
{
|
|
get
|
|
{
|
|
if (_Provider == "MSDAORA")
|
|
_ConnectionType = ConnectionType.Oracle;
|
|
else if (_Provider == "SQLOLEDB")
|
|
_ConnectionType = ConnectionType.SQL;
|
|
else
|
|
_ConnectionType = ConnectionType.Oledb;
|
|
return _ConnectionType;
|
|
}
|
|
set { _ConnectionType = value; }
|
|
}
|
|
|
|
private string _ConnectionString = "";
|
|
|
|
public string ConnectionString
|
|
{
|
|
get
|
|
{
|
|
if(this.ConnectionType== ConnectionType.Oracle)
|
|
_ConnectionString="Data Source=" + _DataSource + ";User ID=" + _UserID + ";Password=" + _Password + ";";
|
|
else if (this.ConnectionType == ConnectionType.SQL)
|
|
{
|
|
if (this._UserID.ToLower() == "windows authentication")
|
|
{
|
|
_ConnectionString = "Data Source=" + this.DataSource + ";Initial Catalog=" + this.DataBase + ";integrated security=yes;Connect Timeout=10000";
|
|
}
|
|
else
|
|
_ConnectionString = "Data Source=" + this.DataSource + ";User ID=" + this.UserID + ";Password=" + this.Password + ";Initial Catalog=" + this.DataBase + ";Connect Timeout=10000";
|
|
}
|
|
else
|
|
_ConnectionString = "Provider=" + this.Provider + ";Data Source=" + this.DataSource + ";User ID=" + this.UserID + ";Password=" + this.Password + ";Initial Catalog=" + this.DataBase + ";Connect Timeout=10000"; ;
|
|
|
|
return _ConnectionString;
|
|
}
|
|
set { _ConnectionString = value; }
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
try
|
|
{
|
|
DataSet config = new DataSet();
|
|
string file = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Connections.xml";
|
|
|
|
if (File.Exists(file))
|
|
{
|
|
config.ReadXml(file);
|
|
//if (config.Tables.Count == 0) return;
|
|
DataTable table = config.Tables["Connection"];
|
|
if (table != null)
|
|
{
|
|
table.PrimaryKey = new DataColumn[] { table.Columns[0] };
|
|
|
|
DataRow row = table.Rows.Find(this.Key);
|
|
if (row == null)
|
|
{
|
|
row = table.NewRow();
|
|
row["Key"] = this.Key;
|
|
row["DataSource"] = this.DataSource;
|
|
row["Provider"] = this.Provider;
|
|
row["Database"] = this.DataBase;
|
|
row["UserID"] = this.UserID;
|
|
row["Password"] = this.Password;
|
|
table.Rows.Add(row);
|
|
table.AcceptChanges();
|
|
config.WriteXml(file);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
table = new DataTable("Connection");
|
|
table.Columns.Add("Key");
|
|
table.Columns.Add("DataSource");
|
|
table.Columns.Add("Provider");
|
|
table.Columns.Add("Database");
|
|
table.Columns.Add("UserID");
|
|
table.Columns.Add("Password");
|
|
|
|
DataRow row = table.NewRow();
|
|
row["Key"] = this.Key;
|
|
row["DataSource"] = this.DataSource;
|
|
row["Provider"] = this.Provider;
|
|
row["Database"] = this.DataBase;
|
|
row["UserID"] = this.UserID;
|
|
row["Password"] = this.Password;
|
|
table.Rows.Add(row);
|
|
|
|
config.Tables.Add(table);
|
|
config.WriteXml(file);
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
DataTable table = new DataTable("Connection");
|
|
table.Columns.Add("Key");
|
|
table.Columns.Add("DataSource");
|
|
table.Columns.Add("Provider");
|
|
table.Columns.Add("Database");
|
|
table.Columns.Add("UserID");
|
|
table.Columns.Add("Password");
|
|
|
|
DataRow row = table.NewRow();
|
|
row["Key"] = this.Key;
|
|
row["DataSource"] = this.DataSource;
|
|
row["Provider"] = this.Provider;
|
|
row["Database"] = this.DataBase;
|
|
row["UserID"] = this.UserID;
|
|
row["Password"] = this.Password;
|
|
table.Rows.Add(row);
|
|
|
|
config.Tables.Add(table);
|
|
config.WriteXml(file);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Failed to Save connection. Error :\n" + ex.Message);
|
|
}
|
|
}
|
|
|
|
public static void ClearAllConnection()
|
|
{
|
|
DataSet config = new DataSet();
|
|
string file = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Connections.xml";
|
|
|
|
if (File.Exists(file))
|
|
{
|
|
config.ReadXml(file);
|
|
if(config.Tables.Count==0)return;
|
|
DataTable table = config.Tables["Connection"];
|
|
if (table != null)
|
|
{
|
|
table.Rows.Clear();
|
|
table.AcceptChanges();
|
|
config.WriteXml(file);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public class Connections : CollectionBase
|
|
{
|
|
public void Add(Connection item)
|
|
{
|
|
InnerList.Add(item);
|
|
}
|
|
|
|
public Connection this[string key]
|
|
{
|
|
get
|
|
{
|
|
foreach (Connection con in this)
|
|
{
|
|
if (con.Key.Equals(key)) return con;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public Connections GetConnections()
|
|
{
|
|
try
|
|
{
|
|
DataSet config = new DataSet();
|
|
string file = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Connections.xml";
|
|
|
|
//adnan for robi and warid deploy easyness
|
|
if (!File.Exists(file))
|
|
{
|
|
DataTable table = new DataTable("Connection");
|
|
table.Columns.Add("Key");
|
|
table.Columns.Add("DataSource");
|
|
table.Columns.Add("Provider");
|
|
table.Columns.Add("Database");
|
|
table.Columns.Add("UserID");
|
|
table.Columns.Add("Password");
|
|
|
|
DataRow row = table.NewRow();
|
|
row["Key"] = ".-WRDRFT-sa";
|
|
row["DataSource"] = @".\SQLExpress";
|
|
row["Provider"] = "SQLOLEDB";
|
|
row["Database"] = "WRDRFT";
|
|
row["UserID"] = "sa";
|
|
row["Password"] = "";
|
|
table.Rows.Add(row);
|
|
|
|
row = table.NewRow();
|
|
row["Key"] = ".-WRDRFT-sa";
|
|
row["DataSource"] = ".";
|
|
row["Provider"] = "SQLOLEDB";
|
|
row["Database"] = "WRDRFT";
|
|
row["UserID"] = "sa";
|
|
row["Password"] = "";
|
|
table.Rows.Add(row);
|
|
|
|
config.Tables.Add(table);
|
|
config.WriteXml(file);
|
|
config.Tables.Remove(table);
|
|
}
|
|
if (File.Exists(file)) //adnan for robi and warid deploy easyness
|
|
{
|
|
config.ReadXml(file);
|
|
if (config.Tables.Contains("Connection"))
|
|
{
|
|
DataTable table = config.Tables["Connection"];
|
|
Connection conection = null;
|
|
foreach (DataRow row in table.Rows)
|
|
{
|
|
conection = new Connection();
|
|
conection.DataSource=row["DataSource"].ToString();
|
|
conection.DataBase = row["Database"].ToString();
|
|
conection.Provider = row["Provider"].ToString();
|
|
conection.UserID = row["UserID"].ToString();
|
|
conection.Password = row["Password"].ToString();
|
|
this.Add(conection);
|
|
}
|
|
}
|
|
}
|
|
return this;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("Failed to Save connection. Error :\n" + ex.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|