private MutiBaseDataContext(Guid OrgID, int?departmentId, stConnectionInfo ci, System.Data.Linq.Mapping.MappingSource map) : base(ci.ConnectionString, map) { OrganizationId = OrgID; DepartmentId = departmentId; UseExternalConnection = false; }
private MutiBaseDataContext(Guid OrgID, int?departmentId, stConnectionInfo ci, System.Data.Linq.Mapping.MappingSource map, TransactionMode UseTransaction) : base(ci.ExternalConnection, map) { OrganizationId = OrgID; DepartmentId = departmentId; UseExternalConnection = true; ExternalConnection = ci.ExternalConnection; ExternalTransaction = ci.ExternalTransaction; base.Transaction = ExternalTransaction; }
//static string MCConnectionString { get { return System.Configuration.ConfigurationManager.ConnectionStrings["Micajah.Common.ConnectionString"].ConnectionString; } } /*static string GetConnectionString(SqlDataReader McReader) * { * string Password = McReader["Password"].ToString(); * string UserName = McReader["UserName"].ToString(); * string ServerName = McReader["ServerName"].ToString(); * string DatabaseName = McReader["DatabaseName"].ToString(); * string Port = McReader["Port"].ToString(); * return "Data Source=" + ServerName + ";Initial Catalog=" + DatabaseName + ";Persist Security Info=True;User ID=" + UserName + ";Password="******"SELECT o.OrganizationId, d.Password, d.UserName, s.Name as ServerName, d.Name as DatabaseName, s.Port " + * "FROM Mc_Organization o " + * "join Mc_Database d on o.DatabaseId=d.DatabaseId " + * "join Mc_DatabaseServer s on d.DatabaseServerId=s.DatabaseServerId " + * "where o.Active=1 and d.Deleted=0 and s.Deleted=0 and " + Where; * McCommand.CommandType = CommandType.Text; * McCommand.Parameters.Add(new SqlParameter(ParamName, ParamValue)); * SqlDataReader McReader = McCommand.ExecuteReader(); * if (!McReader.Read()) throw new Exception("Can not read Micajah Common Database"); * ci.OrganizationId = (Guid)McReader["OrganizationId"]; * ci.ConnectionString = Micajah.Common.Bll.Providers.OrganizationProvider.GetConnectionString(ci.OrganizationId); * } * } * * if (UseExternalConnection) * { * ci.ExternalConnection = new SqlConnection(ci.ConnectionString); * ci.ExternalConnection.Open(); * ci.ExternalTransaction = ci.ExternalConnection.BeginTransaction(ExternalTransactionName); * } * * return ci; * }*/ static stConnectionInfo GetConnection(string OrgSimpleId, bool UseExternalConnection) { stConnectionInfo ci = new stConnectionInfo(); var org = Micajah.Common.Bll.Providers.OrganizationProvider.GetOrganizationByPseudoId(OrgSimpleId); if (org == null || org.OrganizationId == Guid.Empty) { return(ci); } return(GetConnection(org.OrganizationId, UseExternalConnection)); //return GetConnection("o.SimpleId=@SimpleId", "@SimpleId", OrgSimpleId, UseExternalConnection); }
static stConnectionInfo GetConnection(Guid OrgID, bool UseExternalConnection) { stConnectionInfo ci = new stConnectionInfo(); ci.ConnectionString = Micajah.Common.Bll.Providers.OrganizationProvider.GetConnectionString(OrgID); ci.OrganizationId = OrgID; if (UseExternalConnection) { ci.ExternalConnection = new SqlConnection(ci.ConnectionString); ci.ExternalConnection.Open(); ci.ExternalTransaction = ci.ExternalConnection.BeginTransaction(ExternalTransactionName); } return(ci); //return GetConnection("o.OrganizationId=@OrganizationId", "@OrganizationId", OrgID, UseExternalConnection); }
static public MutiBaseDataContext Create(string OrgAlias, string InstAlias, Guid OrgGuid, Guid InstGuid) { stConnectionInfo ci = new stConnectionInfo(); ci.ExternalConnection = null; ci.ExternalTransaction = null; if (OrgGuid == Guid.Empty) { var org = Micajah.Common.Bll.Providers.OrganizationProvider.GetOrganizationByPseudoId(OrgAlias); if (org == null || org.OrganizationId == Guid.Empty) { return(null); } OrgGuid = org.OrganizationId; } ci.ConnectionString = Micajah.Common.Bll.Providers.OrganizationProvider.GetConnectionString(OrgGuid); ci.OrganizationId = OrgGuid; if (InstGuid == Guid.Empty) { var inst = Micajah.Common.Bll.Providers.InstanceProvider.GetInstanceByPseudoId(InstAlias, OrgGuid); if (inst == null || inst.InstanceId == Guid.Empty) { return(null); } InstGuid = inst.InstanceId; } ci.InstanceId = InstGuid; MutiBaseDataContext dc = new MutiBaseDataContext(ci.OrganizationId, null, ci, GetMapping()); int?CompanyID = null; var qq = from c in dc.Tbl_company where c.Company_guid == InstGuid select(int?) c.Company_id; foreach (var q in qq) { CompanyID = q; } dc.DepartmentId = CompanyID; /*MutiBaseDataContext dc = null; * stConnectionInfo ci=new stConnectionInfo(); * if (OrgGuid != Guid.Empty) ci = GetConnection(OrgGuid, false); * if (ci.OrganizationId != Guid.Empty && !string.IsNullOrEmpty(OrgAlias)) ci = GetConnection(OrgAlias, false); * * if (ci.OrganizationId == Guid.Empty && InstGuid!=Guid.Empty) * { * using (SqlConnection McConnection = new SqlConnection(MCConnectionString)) * { * McConnection.Open(); * using (SqlCommand McCommand = McConnection.CreateCommand()) * { * McCommand.CommandText = "SELECT distinct d.Password, d.UserName, s.Name as ServerName, d.Name as DatabaseName, s.Port " + * "FROM Mc_Organization o " + * "join Mc_Database d on o.DatabaseId=d.DatabaseId " + * "join Mc_DatabaseServer s on d.DatabaseServerId=s.DatabaseServerId " + * "where o.Active=1 and d.Deleted=0 and s.Deleted=0"; * McCommand.CommandType = CommandType.Text; * SqlDataReader McReader = McCommand.ExecuteReader(); * List<string> UsedConnectionStrings = new List<string>(); * * Guid FindOrganizationId = Guid.Empty; * string FindConnectionString = null; * int FindDepartmentId=0; * * while (McReader.Read()) * { * string NextConnectionString = GetConnectionString(McReader); * if (UsedConnectionStrings.Contains(NextConnectionString)) continue; * UsedConnectionStrings.Add(NextConnectionString); * * //try * //{ * using (SqlConnection Connection = new SqlConnection(NextConnectionString)) * { * Connection.Open(); * using (SqlCommand Command = Connection.CreateCommand()) * { * Command.CommandType = CommandType.Text; * Command.CommandText = "SELECT i.OrganizationId, c.company_id FROM Mc_Instance i join tbl_Company c on c.company_guid=i.InstanceId where i.InstanceId=@InstanceId and i.Deleted=0"; * Command.Parameters.Add(new SqlParameter("@InstanceId", InstGuid)); * SqlDataReader Reader = Command.ExecuteReader(); * if (!Reader.Read()) continue; * Guid NextOrganizationId = (Guid)Reader["OrganizationId"]; * if (FindOrganizationId != Guid.Empty) * { * if (FindOrganizationId != NextOrganizationId) return null; * continue; * } * FindOrganizationId = NextOrganizationId; * FindConnectionString = NextConnectionString; * FindDepartmentId=(int)Reader["company_id"]; * } * } * //} * //catch { } * } * * ci.OrganizationId = FindOrganizationId; * ci.ConnectionString = FindConnectionString; * ci.InstanceId = InstGuid; * ci.DepartmentId = FindDepartmentId; * } * } * } * * if (ci.OrganizationId == Guid.Empty) return null; * * dc = new MutiBaseDataContext(ci.OrganizationId, null, ci, GetMapping()); * dc.OrganizationId = ci.OrganizationId; * dc.DepartmentId = ci.DepartmentId; * dc.InstaceId = ci.InstanceId; * if(InstGuid!=Guid.Empty && ci.DepartmentId<1) * { * int? CompanyID=null; * var qq = from c in dc.Tbl_company where c.Company_guid == InstGuid select (int?)c.Company_id; * foreach (var q in qq) * { * CompanyID = q; * } * dc.DepartmentId = CompanyID; * }*/ /*if (dc.DepartmentId == null && !string.IsNullOrEmpty(InstAlias)) * { * int? CompanyID = null; * var qq = from i in dc.Mc_Instance join c in dc.Tbl_company on i.InstanceId equals c.Company_guid where i.SimpleId == InstAlias select (int?)c.Company_id; * foreach (var q in qq) * { * CompanyID = q; * } * dc.DepartmentId = CompanyID; * }*/ return(dc); }