connection through File saved on local in asp.net

 using System;

using System.Collections.Generic;

using System.Configuration;

using System.IO;

using System.Text;


using System.Web;

using AI_Portal_Cloud.Utilities;


namespace AI_Portal_Cloud.DAL

{

    class Connection

    {

        public string ConnectionString { get; set; }

        public string Host { get; set; }       

        public Connection()

        {

            string ServerID = GlobalConstant.server_id;

            try

            {                 


                //if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings.Get("Key3")) && string.IsNullOrEmpty(ServerID))

                //{

                //    ServerID = ConfigurationManager.AppSettings.Get("Key3").ToString().Trim();

                //}

                if (string.IsNullOrEmpty(ServerID))

                    ServerID = "0";


                string _ServerFileName = Convert.ToString(ConfigurationManager.AppSettings.Get("key2"));

                string ODBCconnection = (string)Connect(ServerID, _ServerFileName);

                ConnectionString = ODBCconnection;

                

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }


        public string Connect(string strConID, string _ServerFileName)

        {

            try

            {

                string strODBCConnection = string.Empty;

                using (FileStream fileStream = new FileStream(string.IsNullOrEmpty(_ServerFileName) ? @"c:\connection\system_ecdm.txt" : @"c:\connection\" + _ServerFileName + ".txt", FileMode.Open, FileAccess.Read))

                {

                    using (StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8))

                    {

                        string line = String.Empty;

                        while ((line = streamReader.ReadLine()) != null)

                        {

                            string[] strConnnection = line.Split('|');

                            if (strConnnection[0].Trim() == strConID.Trim())

                            {

                                strODBCConnection = strConnnection[1].Trim();

                                if (strConnnection[1].Trim().ToLower().Contains("server="))

                                    return strODBCConnection;

                                else

                                {

                                    strODBCConnection = EncryptStr(strODBCConnection, false);

                                    return strODBCConnection;

                                }

                            }

                        }

                        return strODBCConnection;

                    }

                }

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        private string StrReverse(string expression)

        {

            try

            {

                if (expression == null || expression.Length == 0)

                    return string.Empty;


                string reversedString = string.Empty;

                for (int charIndex = expression.Length - 1; charIndex >= 0; charIndex--)

                {

                    reversedString += expression[charIndex];

                }

                return reversedString;

            }

            catch (Exception ex)

            {

                throw ex;

            }


        }

        private string EncryptStr(string strStr, bool blnEncrypt = false)

        {

            string result = "";

            try

            {

                string strIntrimStr;

                int intCount;


                if (strStr.Trim().Length == 0)

                {

                    return "";

                }


                if (blnEncrypt)

                {

                    strIntrimStr = StrReverse(strStr);

                    // first Level of Encryption

                    strStr = "";

                    for (intCount = 1; intCount <= strIntrimStr.Length; intCount++)

                    {

                        // second Level of Encryption

                        if (strIntrimStr.Trim().Substring(intCount, 1) == "")

                            strStr = strStr + strIntrimStr.Trim().Substring(intCount, 1);

                        else

                        {

                            strStr = strStr + (System.Convert.ToChar(System.Convert.ToInt32(strIntrimStr.Substring(intCount, 1)) + 129));

                        }


                    }

                }

                else

                {

                    strIntrimStr = strStr;

                    strStr = "";

                    for (intCount = 1; intCount <= strIntrimStr.Length; intCount++)

                    {

                        // second Level of Encryption

                        if (strIntrimStr.Trim().Substring(intCount, 1) == "")

                            strStr = strStr + strIntrimStr.Substring(intCount, 1);

                        else

                        {

                            strStr = strStr + (System.Convert.ToChar(System.Convert.ToInt32(strIntrimStr.Substring(intCount, 1)) - 129));

                        }


                    }


                    strStr = StrReverse(strStr);

                }


                result = strStr;

            }

            catch (Exception)

            {

                result = "";

            }


            return result;

        }

    }

}


Comments

Popular posts from this blog

Npgsql query and format with output parameters

Npgsql Helper class