Exemplo n.º 1
0
        public static ActionResult CheckDataConnection(Session session)
        {
            var connString = new ConnectionStringParts
            {
                Server   = session[DataServer],
                Database = session[DataDb],
                User     = session[DataLogin],
                Password = session[DataPassword]
            };

            session["DATACHECKED"] = CheckDbConnection(session, connString)
                ? "Проверка подключения прошла успешно."
                : "Не удалось подключиться к базе данных.";

            return(ActionResult.Success);
        }
Exemplo n.º 2
0
        private static bool CheckDbConnection(Session session, ConnectionStringParts db)
        {
            var connectionString = db.FormConnectionString();

            using (var connection = new SqlConnection(connectionString))
            {
                try
                {
                    connection.Open();
                    return(true);
                }
                catch (Exception ex)
                {
                    session.Log("Error ocured during connection string checking: {0}", ex.Message);
                    return(false);
                }
            }
        }
Exemplo n.º 3
0
        public string ParseConnectionString(string ConnectionString, ConnectionStringParts ItemType)
        {
            int           iToken       = 0;
            string        Token        = string.Empty;
            int           iSearch      = 0;
            List <string> SearchString = new List <string> {
                ConnectionString
            };
            string ExtendedProperties = string.Empty;

            //These connection strings change with one version of ADO to the next, so we must be flexible here...
            //The idea is to build a list of property strings, the first of which is the passed connection string itself.
            //Any property named "EXTENDED PROPERTIES" will be ignored, so we can treat them separately. Those extended properties
            //will be processed as their own string if the desired property isn't otherwise found...

            if (ConnectionString.ToUpper().Contains("EXTENDED PROPERTIES"))
            {
                ExtendedProperties = string.Empty;
                for (iToken = 1; iToken <= ConnectionString.Tokens(";"); iToken++)
                {
                    Token = ConnectionString.Parse(iToken, ";", "\"\"");
                    //Debug.Print "Token #" & iToken & ": " & Token
                    if (Token.Substring(0, "EXTENDED PROPERTIES".Length) == "EXTENDED PROPERTIES")
                    {
                        break;
                    }
                }
                if (Token.Substring(0, "EXTENDED PROPERTIES".Length) != "EXTENDED PROPERTIES")
                {
                    return(string.Empty);
                }
                ExtendedProperties = Token.Substring("EXTENDED PROPERTIES=".Length + 1);
                SearchString.Add(ExtendedProperties);
            }

            for (iSearch = SearchString.Count; iSearch >= 0; iSearch--)
            {
                iToken = 1;
                Token  = SearchString[iSearch - 1];
                while (Token != string.Empty)
                {
                    Token = SearchString[iSearch - 1].Parse(iToken, ";", "\"\"");
                    if (Token != string.Empty)
                    {
                        switch (Token.ToUpper().Substring(0, Token.IndexOf("=")))
                        {
                        case "DRIVER":
                        case "PROVIDER":
                            if (ItemType == ConnectionStringParts.Driver)
                            {
                                string temp = Token.Substring(Token.IndexOf("=") + 1);
                                if (temp.Left(1) == "{")
                                {
                                    temp = temp.Substring(1, temp.Length - 2);
                                }
                                return(temp);
                            }
                            break;

                        case "UID":
                        case "USER ID":
                            if (ItemType == ConnectionStringParts.UserID)
                            {
                                return(Token.Substring(Token.IndexOf("=") + 1));
                            }
                            break;

                        case "PASSWORD":        //SQL Server
                            if (ItemType == ConnectionStringParts.Password)
                            {
                                return(Token.Substring(Token.IndexOf("=") + 1));
                            }
                            break;

                        case "SERVER":          //SQL Server
                            if (ItemType == ConnectionStringParts.Server)
                            {
                                return(Token.Substring(Token.IndexOf("=") + 1));
                            }
                            break;

                        case "DATABASE":        //SQL Server
                            if (ItemType == ConnectionStringParts.Database)
                            {
                                return(Token.Substring(Token.IndexOf("=") + 1));
                            }
                            break;

                        case "DATA SOURCE":     //SQL Server
                            if (ItemType == ConnectionStringParts.Server)
                            {
                                return(Token.Substring(Token.IndexOf("=") + 1));
                            }
                            break;

                        default:
                            break;
                        }
                    }
                    iToken++;
                }
            }
            return(string.Empty);
        }