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