Esempio n. 1
0
        public int RestoreOrCreate(string bakfile, string path = null, string dbname = null)
        {
            string sql2;

            if (string.IsNullOrEmpty(dbname))
            {
                dbname = Path.GetFileNameWithoutExtension(bakfile);
                sql2   = "RESTORE DATABASE [" + dbname + "] FROM  DISK = N'" + bakfile + "' \nWITH";
                string sql = "RESTORE FILELISTONLY  FROM DISK = '" + bakfile + "';";
                if (string.IsNullOrEmpty(path))
                {
                    path = Path.GetDirectoryName(bakfile);
                }
                using (DbDataReader reader = GetReader(sql))
                {
                    while (reader.Read())
                    {
                        string name = reader.ReadString("LogicalName");
                        string db   = reader.ReadString("PhysicalName");
                        //替换路径

                        string ex   = Path.GetExtension(db);
                        string file = PathHelper.Combine(path, name + ex);
                        sql2 += "\nMOVE N'" + name + "' TO N'" + file + "',";
                    }
                    if (sql2.EndsWith(","))
                    {
                        sql2 = sql2.Substring(0, sql2.Length - 1);
                    }
                }
            }
            else
            {
                sql2 = "RESTORE DATABASE " + dbname + " FROM  DISK = N'" + bakfile + "'";
            }
            //Console.WriteLine(sql2);
            //File.WriteAllText("resotre.sql", sql2);
            return(ExcuteSQL(sql2));
        }