Esempio n. 1
0
        public void CreateConnectionString(ServerConfiguration serverConfiguration)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
            ConnectionStringSettings connectionstringsets = new ConnectionStringSettings();
            connectionstringsets.Name = "filth1connection";

            // create connection string
            if (serverConfiguration.Live)
            {
                connectionstringsets.ConnectionString = "Data Source=" + serverConfiguration.ServerName + ";Initial Catalog=" + serverConfiguration.Catalog + ";Integrated Security=false;User ID=" + serverConfiguration.Username + ";Password="******";multipleactiveresultsets=True;App=EntityFramework;"; //Encrypt=yes
                try
                {
                    using (var connection = new SqlConnection(connectionstringsets.ConnectionString))
                    {
                        connection.Open();
                        // most hostings doesn't allow this
                        // var command = connection.CreateCommand();
                        // command.CommandText = "CREATE DATABASE " + database;
                        // command.ExecuteNonQuery();
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            else
                connectionstringsets.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDBFilename=|DataDirectory|filthdb_" + new Random().Next() + ".mdf" + ";User Instance=true;Integrated Security=true;multipleactiveresultsets=True;App=EntityFramework";

            connectionstringsets.ProviderName = "System.Data.SqlClient";

            config.ConnectionStrings.ConnectionStrings.Add(connectionstringsets);

            config.Save();
        }
Esempio n. 2
0
        public HttpResponseMessage Server(ServerConfiguration serverConfiguration)
        {
            FilthConfiguration configuration = new FilthConfiguration();

            ConnectionStringState state = configuration.CheckConnection();
            var response = new HttpResponseMessage();

            switch (configuration.CheckConnection())
            {
                case ConnectionStringState.Absent:
                    if (ModelState.IsValid)
                    {
                        configuration.CreateConnectionString(serverConfiguration);

                        response = new HttpResponseMessage(HttpStatusCode.OK);
                        response.Headers.Location = new Uri(Request.RequestUri.Authority + "/?install=2");
                    } else
                        response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
                    break;
                case ConnectionStringState.Present:
                    response = new HttpResponseMessage(HttpStatusCode.OK);
                    response.Headers.Location = new Uri(Request.RequestUri.Authority + "/?install=0");
                    break;
                case ConnectionStringState.Invalid:
                    response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, new Exception("I can't connect to database, either it doesn't exist or your credentials are out of date."));
                    break;
            }

            return response;
        }