net 对mssql 的操作(备份,还原,获取表数据,获取表字段信息)

 #region 数据库管理
        /// <summary>
        /// 还原恢复数据库
        /// </summary>
        /// <param name="FilePath">保存路径</param>
        /// <returns></returns>
        public bool DataRestore(string FilePath)
        {
            string[] Connection = ConfigHelper.GetAppSettings("SqlServer_WYL_DB").Split(';');
            SqlServerBackup sqlserverbackup = new SqlServerBackup();
            sqlserverbackup.Server = Connection[0].Substring(7);
            sqlserverbackup.Database = Connection[1].Substring(9);
            sqlserverbackup.Uid = Connection[2].Substring(4);
            sqlserverbackup.Pwd = Connection[3].Substring(4);
            if (sqlserverbackup.DbRestore(FilePath))
                return true;
            else
                return false;
        }
        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="FilePath">保存路径</param>
        /// <returns></returns>
        public bool DataBackups(string FilePath)
        {
            try
            {
                string[] Connection = ConfigHelper.GetAppSettings("SqlServer_WYL_DB").Split(';');
                SqlServerBackup sqlserverbackup = new SqlServerBackup();
                sqlserverbackup.Server = Connection[0].Substring(7);
                sqlserverbackup.Database = Connection[1].Substring(9);
                sqlserverbackup.Uid = Connection[2].Substring(4);
                sqlserverbackup.Pwd = Connection[3].Substring(4);
                if (sqlserverbackup.DbBackup(FilePath))
                    return true;
                else
                    return false;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 添加数据库备份恢复记录
        /// </summary>
        /// <param name="Type">类型:备份、恢复</param>
        /// <param name="File">文件名</param>
        /// <param name="Size">文件大小</param>
        /// <param name="CreateUserName">创建用户</param>
        /// <param name="DB">所属数据库</param>
        /// <param name="Memo">备注</param>
        /// <returns></returns>
        public void Add_Backup_Restore_Log(string Type, string File, string Size, string CreateUserName, string DB, string Memo)
        {
            LogHelper Logger = new LogHelper("Backup_Restore_Log");
            Hashtable ht = new Hashtable();
            StringBuilder sb = new StringBuilder();
            sb.Append(Type + "∫");
            sb.Append(File + "∫");
            sb.Append(Size + "∫");
            sb.Append(CreateUserName + "∫");
            sb.Append(DB + "∫");
            sb.Append(Memo + "∫");
            sb.Append(DateTime.Now + "∫");
            sb.Append("∮");
            Logger.WriteLog(sb.ToString());
        }
        /// <summary>
        /// 数据库备份还原记录
        /// </summary>
        /// <returns></returns>
        public DataTable GetBackup_Restore_Log_List()
        {
            LogHelper Logger = new LogHelper("Backup_Restore_Log");
            string filepath = ConfigHelper.GetAppSettings("LogFilePath") + "/Backup_Restore_Log.log";
            StreamReader sr = new StreamReader(filepath, Encoding.GetEncoding("UTF-8"));//取得这txt文件的编码
            string[] strvalue = sr.ReadToEnd().ToString().Split('∮');
            sr.Close();
            DataTable dt = new DataTable();
            dt.Columns.Add("Backup_Restore_Type", Type.GetType("System.String"));
            dt.Columns.Add("Backup_Restore_File", Type.GetType("System.String"));
            dt.Columns.Add("Backup_Restore_Size", Type.GetType("System.String"));
            dt.Columns.Add("CreateUserName", Type.GetType("System.String"));
            dt.Columns.Add("Backup_Restore_DB", Type.GetType("System.String"));
            dt.Columns.Add("Backup_Restore_Memo", Type.GetType("System.String"));
            dt.Columns.Add("CreateDate", Type.GetType("System.String"));
            foreach (string item in strvalue)
            {
                if (item.Length > 6)
                {
                    string[] str_item = item.Split('∫');
                    DataRow row = dt.NewRow();
                    string[] Typeitem = str_item[0].Split(']');
                    row["Backup_Restore_Type"] = Typeitem[1].Trim();
                    row["Backup_Restore_File"] = str_item[1];
                    row["Backup_Restore_Size"] = str_item[2];
                    row["CreateUserName"] = str_item[3];
                    row["Backup_Restore_DB"] = str_item[4];
                    row["Backup_Restore_Memo"] = str_item[5];
                    row["CreateDate"] = str_item[6];
                    dt.Rows.Add(row);
                }
            }
            dt.DefaultView.Sort = "CreateDate DESC";//DataTable排序
            DataTable dtTemp = dt.DefaultView.ToTable();
            return dtTemp;
        }
        /// <summary>
        /// 获取数据库所有表名
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysobjects()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Name as TABLE_NAME from sysobjects where xtype='u' and status >=0 and Name !='sysdiagrams' ");
            return DataFactory.SqlDataBase().GetDataTableBySQL(strSql);
        }
        /// <summary>
        /// 获取某一个表的所有字段
        /// </summary>
        /// <param name="object_id">表名</param>
        /// <returns></returns>
        public DataTable GetSyscolumns(string object_id)
        {
            DataTable dt = new DataTable();
            StringBuilder strSql = new StringBuilder();
            if (!string.IsNullOrEmpty(object_id) && object_id != "未选择")
            {
                strSql.Append(@"SELECT
                                     [列名]=a.name,
                                     [数据类型]=b.name,
                                     [长度]=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
                                     [是否为空]=case when a.isnullable=1 then '√'else '' end,
                                     [默认值]=isnull(e.text,''),
                                     [说明]=isnull(g.[value],'未填说明')
                                     FROM syscolumns a
                                     left join systypes b on a.xusertype=b.xusertype
                                     inner join sysobjects d on a.id=d.id  and d.xtype='U' and  d.name<>'dtproperties'
                                     left join syscomments e on a.cdefault=e.id
                                     left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id 
                                     left join sys.extended_properties f on d.id=f.major_id and f.minor_id=0");
                strSql.Append("where d.name='" + object_id + "' order by a.id,a.colorder");
                return DataFactory.SqlDataBase().GetDataTableBySQL(strSql);
            }
            return dt;
        }
        #endregion

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。