public ActionResult Step2_DatabaseInfo()
        {
            DataBaseInfoModel model = new DataBaseInfoModel();
            if (TempData["TempModel"] != null)
            {
                model = TempData["TempModel"] as DataBaseInfoModel;
                TempData["TempModel"] = TempData["TempModel"];
            }

            return View(model);
        }
Exemple #2
0
        public ActionResult Step2_DatabaseInfo()
        {
            DataBaseInfoModel model = new DataBaseInfoModel();

            if (TempData["TempModel"] != null)
            {
                model = TempData["TempModel"] as DataBaseInfoModel;
                TempData["TempModel"] = TempData["TempModel"];
            }

            return(View(model));
        }
Exemple #3
0
        public JsonResult _Step2_Wait(DataBaseInfoModel model)
        {
            string server = string.Format("{0}{1}{2}", model.Server
                                          , !string.IsNullOrEmpty(model.Port) ? ":" + model.Port : ""
                                          , !string.IsNullOrEmpty(model.Instance) ? "\\" + model.Instance : "");

            String connectString = string.Format("server={0};uid={1};pwd={2};Trusted_Connection=no",
                                                 server, model.DataBaseUserName, model.DataBasePassword);


            TempData["TempModel"] = model;
            ConcurrentDictionary <string, string> messages = new ConcurrentDictionary <string, string>();
            //检测服务器是否可以连接上
            SqlConnection sqlConnection = GetSqlConnection(connectString, out messages);

            if (sqlConnection == null)
            {
                if (string.IsNullOrEmpty(model.Instance))
                {
                    connectString = string.Format("server={0};uid={1};pwd={2};Trusted_Connection=no",
                                                  model.Server + "\\SQLEXPRESS", model.DataBaseUserName, model.DataBasePassword);
                    sqlConnection = GetSqlConnection(connectString, out messages);
                }
            }

            if (sqlConnection == null)
            {
                TempData["Error"] = messages;
                return(Json(new { }));
            }

            //尝试打开数据库链接
            try
            {
                sqlConnection.Open();
            }
            catch (Exception e)
            {
                sqlConnection.Close();
                messages[e.Message] = e.StackTrace;
                TempData["Error"]   = messages;
                return(Json(new { }));
            }

            SqlCommand command = new SqlCommand("select @@Version", sqlConnection);
            var        val     = command.ExecuteScalar();


            if (val == null || string.IsNullOrEmpty(val.ToString()))
            {
                messages["要求数据库为Sql 2005及以上"] = string.Empty;
                TempData["Error"]             = messages;

                return(Json(new { }));
            }

            int dbVersion = Convert.ToInt32(val.ToString().Substring(21, 4));

            if (dbVersion < 2005)
            {
                messages["要求数据库为Sql 2005及以上,当前为" + val] = string.Empty;
                TempData["Error"] = messages;

                return(Json(new { }));
            }


            command = new SqlCommand(string.Format("select 1 from master..sysdatabases where [name]='{0}'", model.DataBase), sqlConnection);
            val     = command.ExecuteScalar();

            //创建空数据库
            if (val == null)
            {
                command.CommandText = string.Format(" create database {0}; ALTER DATABASE {0} SET RECOVERY SIMPLE; ", model.DataBase);

                try
                {
                    command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    sqlConnection.Close();
                    messages[e.Message] = e.StackTrace;
                    TempData["Error"]   = messages;
                    return(Json(new { }));
                }
                sqlConnection.Close();
            }
            else
            {
                try
                {
                    sqlConnection.Close();
                    sqlConnection = GetSqlConnection(connectString + ";database=" + model.DataBase, out messages);
                    sqlConnection.Open();
                    command.Connection  = sqlConnection;
                    command.CommandText = "select count(*) from sysobjects where (xtype = 'u')";

                    int count = 0;
                    int.TryParse(command.ExecuteScalar().ToString(), out count);

                    if (count > 1)
                    {
                        command.CommandText = "select count(1) from tn_SystemData";
                        command.Connection  = sqlConnection;
                        command.ExecuteNonQuery();
                    }
                }
                catch (Exception e)
                {
                    sqlConnection.Close();
                    messages["当前数据库不是本程序数据库或一个空库!"] = "";
                    TempData["Error"] = messages;
                    return(Json(new { }));
                }
                sqlConnection.Close();
            }
            //修改web.config中数据库链接字符串
            connectString += ";database=" + model.DataBase;

            SetWebConfig(connectString, out messages);
            return(Json(new { success = true, connectString = connectString }));
        }
Exemple #4
0
 public ActionResult Step2_Wait(DataBaseInfoModel model)
 {
     return(View(model));
 }
Exemple #5
0
        public JsonResult _Step2_Wait(DataBaseInfoModel model)
        {
            string server = string.Format("{0}{1}{2}", model.Server
                                          , !string.IsNullOrEmpty(model.Port) ? ":" + model.Port : ""
                                          , !string.IsNullOrEmpty(model.Instance) ? "\\" + model.Instance : "");

            string connectString = string.Format("server={0};uid={1};pwd={2};",
                                                 server, model.DataBaseUserName, model.DataBasePassword);

            if (model.DBType == DBType.MySql)
            {
                connectString += "Charset=utf8;";
            }

            TempData["TempModel"] = model;
            ConcurrentDictionary <string, string> messages = new ConcurrentDictionary <string, string>();

            Database db = CreateDatabase(connectString, model.DBType, ref messages);

            //尝试打开数据库链接,检查数据库是否能够链接上
            try
            {
                db.OpenSharedConnection();
                db.CloseSharedConnection();
            }
            catch (Exception e)
            {
                bool success = false;
                //如果是SQL Server数据库,则再次尝试打开SQLEXPRESS
                if (model.DBType == DBType.SqlServer && string.IsNullOrEmpty(model.Instance))
                {
                    try
                    {
                        connectString = string.Format("server={0};uid={1};pwd={2};",
                                                      model.Server + "\\SQLEXPRESS", model.DataBaseUserName, model.DataBasePassword);
                        db = CreateDatabase(connectString, model.DBType, ref messages);
                        db.OpenSharedConnection();
                        db.CloseSharedConnection();
                        success = true;
                    }
                    catch { }
                }
                if (!success)
                {
                    messages["数据库帐号或密码错误,无法登录数据库服务器"] = e.Message;
                    TempData["Error"] = messages;
                    return(Json(new { }));
                }
            }

            if (model.DBType == DBType.SqlServer)
            {
                #region SQL Server

                var val = db.FirstOrDefault <string>("select @@@@Version");

                if (string.IsNullOrEmpty(val))
                {
                    messages["要求数据库为Sql 2005及以上"] = string.Empty;
                    TempData["Error"]             = messages;
                    return(Json(new { }));
                }

                int dbVersion = Convert.ToInt32(val.Substring(21, 4));
                if (dbVersion < 2005)
                {
                    messages["要求数据库为Sql 2005及以上,当前为" + val] = string.Empty;
                    TempData["Error"] = messages;
                    return(Json(new { }));
                }

                val = db.FirstOrDefault <string>("select 1 from master..sysdatabases where [name]=@0", model.DataBase);

                //创建空数据库
                if (string.IsNullOrEmpty(val))
                {
                    try
                    {
                        db.Execute(string.Format("create database {0}; ALTER DATABASE {0} SET RECOVERY SIMPLE; ", model.DataBase));
                    }
                    catch (Exception e)
                    {
                        messages[e.Message] = e.StackTrace;
                        TempData["Error"]   = messages;
                        return(Json(new { }));
                    }
                }
                else
                {
                    //检查当前数据库是否为本程序数据库或一个空库
                    string dbConnectString = connectString + ";database=" + model.DataBase;
                    db = CreateDatabase(dbConnectString, model.DBType, ref messages);

                    int tableCount = db.FirstOrDefault <int>("select COUNT(*) from sysobjects where xtype='U'");

                    string tableName = db.FirstOrDefault <string>("select name from sysobjects where name=@0", "tn_SystemData");

                    if (tableCount > 0 && string.IsNullOrEmpty(tableName))
                    {
                        messages["当前数据库不是本程序数据库!"] = string.Empty;
                        TempData["Error"]          = messages;
                        return(Json(new { }));
                    }
                }
                #endregion
            }
            else if (model.DBType == DBType.MySql)
            {
                #region MySql
                string information_schema_ConnectString = connectString + "database=information_schema;";
                db = CreateDatabase(information_schema_ConnectString, model.DBType, ref messages);

                //检查数据库是否已创建
                string SCHEMA_NAME = db.FirstOrDefault <string>(Sql.Builder.Select("SCHEMA_NAME").From("SCHEMATA").Where("SCHEMA_NAME=@0", model.DataBase));
                if (string.IsNullOrEmpty(SCHEMA_NAME))
                {
                    //创建空数据库
                    db.Execute(string.Format("CREATE DATABASE `{0}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ", model.DataBase));
                }
                else
                {
                    int tableCount = db.FirstOrDefault <int>("SELECT COUNT(*) FROM information_schema.TABLES where TABLE_SCHEMA = '@0';", model.DataBase);

                    //当前数据库不是本程序数据库或一个空库
                    string TABLE_NAME = db.FirstOrDefault <string>(Sql.Builder.Select("TABLE_NAME").From("TABLES").Where("TABLE_NAME=\"tn_SystemData\""));
                    if (tableCount > 0 && string.IsNullOrEmpty(SCHEMA_NAME))
                    {
                        messages["当前数据库不是本程序数据库!"] = string.Empty;
                        TempData["Error"]          = messages;
                        return(Json(new { }));
                    }
                }

                #endregion
            }
            //修改web.config中数据库链接字符串
            connectString += "database=" + model.DataBase;

            SetWebConfig(connectString, model.DBType, out messages);
            return(Json(new { success = true, connectString = connectString, DBType = model.DBType }));
        }
 public ActionResult Step2_Wait(DataBaseInfoModel model)
 {
     return View(model);
 }
        public JsonResult _Step2_Wait(DataBaseInfoModel model)
        {
            string server = string.Format("{0}{1}{2}", model.Server
                                                     , !string.IsNullOrEmpty(model.Port) ? ":" + model.Port : ""
                                                     , !string.IsNullOrEmpty(model.Instance) ? "\\" + model.Instance : "");

            string connectString = string.Format("server={0};uid={1};pwd={2};",
                                                 server, model.DataBaseUserName, model.DataBasePassword);

            if (model.DBType == DBType.MySql)
            {
                connectString += "Charset=utf8;";
            }

            TempData["TempModel"] = model;
            ConcurrentDictionary<string, string> messages = new ConcurrentDictionary<string, string>();

            Database db = CreateDatabase(connectString, model.DBType, ref messages);

            //尝试打开数据库链接,检查数据库是否能够链接上
            try
            {
                db.OpenSharedConnection();
                db.CloseSharedConnection();
            }
            catch (Exception e)
            {
                bool success = false;
                //如果是SQL Server数据库,则再次尝试打开SQLEXPRESS
                if (model.DBType == DBType.SqlServer && string.IsNullOrEmpty(model.Instance))
                {
                    try
                    {
                        connectString = string.Format("server={0};uid={1};pwd={2};",
                                                      model.Server + "\\SQLEXPRESS", model.DataBaseUserName, model.DataBasePassword);
                        db = CreateDatabase(connectString, model.DBType, ref messages);
                        db.OpenSharedConnection();
                        db.CloseSharedConnection();
                        success = true;
                    }
                    catch { }
                }
                if (!success)
                {
                    messages["数据库帐号或密码错误,无法登录数据库服务器"] = e.Message;
                    TempData["Error"] = messages;
                    return Json(new { });
                }
            }

            if (model.DBType == DBType.SqlServer)
            {
                #region SQL Server

                var val = db.FirstOrDefault<string>("select @@@@Version");

                if (string.IsNullOrEmpty(val))
                {
                    messages["要求数据库为Sql 2005及以上"] = string.Empty;
                    TempData["Error"] = messages;
                    return Json(new { });
                }

                int dbVersion = Convert.ToInt32(val.Substring(21, 4));
                if (dbVersion < 2005)
                {
                    messages["要求数据库为Sql 2005及以上,当前为" + val] = string.Empty;
                    TempData["Error"] = messages;
                    return Json(new { });
                }

                val = db.FirstOrDefault<string>("select 1 from master..sysdatabases where [name]=@0", model.DataBase);

                //创建空数据库
                if (string.IsNullOrEmpty(val))
                {
                    try
                    {
                        db.Execute(string.Format("create database {0}; ALTER DATABASE {0} SET RECOVERY SIMPLE; ", model.DataBase));
                    }
                    catch (Exception e)
                    {
                        messages[e.Message] = e.StackTrace;
                        TempData["Error"] = messages;
                        return Json(new { });
                    }
                }
                else
                {
                    //检查当前数据库是否为本程序数据库或一个空库
                    string dbConnectString = connectString + ";database=" + model.DataBase;
                    db = CreateDatabase(dbConnectString, model.DBType, ref messages);

                    int tableCount = db.FirstOrDefault<int>("select COUNT(*) from sysobjects where xtype='U'");

                    string tableName = db.FirstOrDefault<string>("select name from sysobjects where name=@0", "tn_SystemData");

                    if (tableCount > 0 && string.IsNullOrEmpty(tableName))
                    {
                        messages["当前数据库不是本程序数据库!"] = string.Empty;
                        TempData["Error"] = messages;
                        return Json(new { });
                    }
                }
                #endregion
            }
            else if (model.DBType == DBType.MySql)
            {
                #region MySql
                string information_schema_ConnectString = connectString + "database=information_schema;";
                db = CreateDatabase(information_schema_ConnectString, model.DBType, ref messages);

                //检查数据库是否已创建
                string SCHEMA_NAME = db.FirstOrDefault<string>(Sql.Builder.Select("SCHEMA_NAME").From("SCHEMATA").Where("SCHEMA_NAME=@0", model.DataBase));
                if (string.IsNullOrEmpty(SCHEMA_NAME))
                {
                    //创建空数据库
                    db.Execute(string.Format("CREATE DATABASE `{0}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; ", model.DataBase));
                }
                else
                {
                    int tableCount = db.FirstOrDefault<int>("SELECT COUNT(*) FROM information_schema.TABLES where TABLE_SCHEMA = '@0';", model.DataBase);

                    //当前数据库不是本程序数据库或一个空库
                    string TABLE_NAME = db.FirstOrDefault<string>(Sql.Builder.Select("TABLE_NAME").From("TABLES").Where("TABLE_NAME=\"tn_SystemData\""));
                    if (tableCount > 0 && string.IsNullOrEmpty(SCHEMA_NAME))
                    {
                        messages["当前数据库不是本程序数据库!"] = string.Empty;
                        TempData["Error"] = messages;
                        return Json(new { });
                    }
                }

                #endregion
            }
            //修改web.config中数据库链接字符串
            connectString += "database=" + model.DataBase;

            SetWebConfig(connectString, model.DBType, out messages);
            return Json(new { success = true, connectString = connectString, DBType = model.DBType });
        }