yantian yue
2023-12-28 fd5f7c86beec35f6b7cde3f16f23c6cf1279a447
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using YX.Entity;
namespace YX.DAL
{
    public static class System_Dal
    {
        
        /// <summary>
        /// 获取数据库所有表名
        /// </summary>
        /// <returns></returns>
        public static List<string > GetTableName()
        {
            List<string>list=new List<string>();
            try
            {
                using (Sam_DBEntities db=new Sam_DBEntities())
                {
                    string sql = @"select Name as TABLE_NAME from sysobjects where xtype = 'u' and status >= 0 and Name != 'sysdiagrams'";
 
                    DataTable dt= SqlHelper.ExecuteDataset(db.Database.Connection.ConnectionString, CommandType.Text, sql).Tables[0];
                    if (dt != null)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            list.Add(dr["TABLE_NAME"].ToString());
                        }
                    }
 
                    return list;
                }
 
       
            }
            catch (Exception ex)
            {
 
                throw ex;
            }
        }
        /// <summary>
        /// 获取某一个表的所有字段
        /// </summary>
        /// <param name="object_id">表名</param>
        /// <returns></returns>
        public static DataTable GetSyscolumns(string object_id)
        {
            DataTable dt = new DataTable();
            try
            {
                using (Sam_DBEntities db=new Sam_DBEntities())
                {
                    string strSql = "";
                    if (!string.IsNullOrEmpty(object_id) && object_id != "未选择")
                    {
                        strSql = @"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
                        where d.name='" + object_id + "' order by a.id,a.colorder";
                        return SqlHelper.ExecuteDataset(db.Database.Connection.ConnectionString, CommandType.Text, strSql).Tables[0];
                    }
                }
                    
            }
            catch (Exception)
            {
 
                throw;
            }
            return dt;
        }
        public static int DbBackup(string FilePath)
        {
            try
            {
                using (Sam_DBEntities db=new Sam_DBEntities())
                {
                    string sql = string.Format(@"BACKUP DATABASE Sam_DB
                      TO  DISK = N'{0}'
                      WITH NOFORMAT
                      , NOINIT
                      , NAME = N'Sam_DB'
                      , SKIP
                      , REWIND
                      , NOUNLOAD
                      , STATS = 10", FilePath);
 
                    int i = SqlHelper.ExecuteNonQuery(db.Database.Connection.ConnectionString, CommandType.Text, sql);
                    return i;
                }
    
            }
            catch (Exception ex)
            {
 
                throw ex;
            }
        }
        /// <summary>
        /// 日志写数据库中
        /// </summary>
        /// <param name="loginfo"></param>
        /// <returns></returns>
        public static int WriteLogToDB(Base_Log loginfo)
        {
            try
            {
                using (Sam_DBEntities db=new Sam_DBEntities())
                {
                    loginfo.CreateTime = DateTime.Now;
                    loginfo.LogID = Guid.NewGuid().ToString();
                    db.Base_Log.Add(loginfo);
                    return db.SaveChanges(); ;
                }
            }
            catch (Exception ex)
            {
 
                throw ex;
            }
        }
 
        public static DataTable GetSystemLog(string ip ,string type) 
        {
            DataTable dt = new DataTable();
            try
            {
                using (Sam_DBEntities db = new Sam_DBEntities()) 
                {
                    var sql = @"SELECT top 35 LogMessage,CreateTime FROM Base_Log where type='" + type + "' and LocalIP= '" + ip+ "'  ORDER BY CreateTime DESC ";
                    dt = SqlHelper.ExecuteDataset(db.Database.Connection.ConnectionString, CommandType.Text, sql).Tables[0];
                }
            }
            catch (Exception ex)
            {
 
                throw ex;
            }
            return dt;
        }
    }
}