深度优先

这个家伙好懒,除了文章什么都没留下

0%

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
public static CookieContainer theCC = new CookieContainer();

/// <summary>
/// 登录方法(无验证码)
/// </summary>
/// <PARAM name="url">POST请求的地址</PARAM>
/// <PARAM name="paramList">参数列表 例如 name=zhangsan&pass=lisi</PARAM>
/// <PARAM name="referer">来源地址</PARAM>
/// <RETURNS></RETURNS>
public static string Login(String url, String paramList, string referer)
{
HttpWebResponse res = null;
HttpWebRequest req = null;
string strResult = "";
try
{
req = (HttpWebRequest)WebRequest.Create(url);
//配置请求header
req.Headers.Add(HttpRequestHeader.AcceptCharset, "GBK,utf-8;q=0.7,*;q=0.3");
req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
req.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.8");
req.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
req.KeepAlive = true;
req.Referer = referer;
req.Headers.Add(HttpRequestHeader.CacheControl, "max-age=0");
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.5 Safari/534.7";
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.AllowAutoRedirect = true;
//设置cookieContainer用来接收cookie
req.CookieContainer = theCC;
StringBuilder UrlEncoded = new StringBuilder();
//对参数进行encode
Char[] reserved = { '?', '=', '&' };
byte[] SomeBytes = null;
if (paramList != null)
{
int i = 0, j;
while (i < paramList.Length)
{
j = paramList.IndexOfAny(reserved, i);
if (j == -1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));
UrlEncoded.Append(paramList.Substring(j, 1));
i = j + 1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}
//返回请求
res = (HttpWebResponse)req.GetResponse();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
Stream responseStream = null;
if (res.ContentEncoding.ToLower() == "gzip")
{
responseStream = new System.IO.Compression.GZipStream(res.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
}
else if (res.ContentEncoding.ToLower() == "deflate")
{
responseStream = new System.IO.Compression.DeflateStream(res.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
}
else
{
responseStream = res.GetResponseStream();
}
StreamReader sr = new StreamReader(responseStream, encode);
strResult = sr.ReadToEnd();
}
catch (Exception e)
{
//writeLog
}
finally
{
res.Close();
}
return strResult;
}

/// <summary> 获取页面HTML
///
/// <PARAM name="url"></PARAM>
/// <PARAM name="paramList"></PARAM>
/// <RETURNS></RETURNS>
public static string getPage(String url, string referer)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string strResult = string.Empty;
req.Headers["If-None-Match"] = "36d0ed736e88c71:d9f";
req.Referer = referer;
req.CookieContainer = theCC;
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = null;
try
{
sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
strResult = sr.ReadToEnd();
}
catch (System.Exception ex)
{
//writeLog
}
finally
{
sr.Close();
}
return strResult;
}

/// <summary>
/// 模仿异步请求POST的方法
/// </summary>
/// <PARAM name="url"></PARAM>
/// <PARAM name="referer"></PARAM>
/// <PARAM name="methed"></PARAM>
/// <PARAM name="paramList"></PARAM>
/// <RETURNS></RETURNS>
public static string VisitPage(string url, string referer, string paramList)
{
HttpWebResponse response = null;
string strResult = string.Empty;
try
{
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.KeepAlive = true;
request.Referer = referer;
request.Headers.Add(HttpRequestHeader.AcceptCharset, "GBK,utf-8;q=0.7,*;q=0.3");
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate,sdch");
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.8");
request.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.5 Safari/534.7";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = theCC;
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
StringBuilder UrlEncoded = new StringBuilder();
//对参数进行encode
Char[] reserved = { '?', '=', '&' };
byte[] SomeBytes = null;
if (paramList != null)
{
int i = 0, j;
while (i < paramList.Length)
{
j = paramList.IndexOfAny(reserved, i);
if (j == -1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));
UrlEncoded.Append(paramList.Substring(j, 1));
i = j + 1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
request.ContentLength = SomeBytes.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
response = (HttpWebResponse)request.GetResponse();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
Stream responseStream = null;
if (response.ContentEncoding.ToLower() == "gzip")
{
responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
}
else if (response.ContentEncoding.ToLower() == "deflate")
{
responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
}
else
{
responseStream = response.GetResponseStream();
}
StreamReader sr = new StreamReader(responseStream, encode);
strResult = sr.ReadToEnd();
}
catch
{
//dosomething
}
finally
{
response.Close();
}
return strResult;
}

/// <summary>
/// Http下载文件
/// </summary>
public static string HttpDownloadFile(string url, string path)
{
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream responseStream = response.GetResponseStream();
//创建本地文件写入流
Stream stream = new FileStream(path, FileMode.Create);
byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
stream.Close();
responseStream.Close();
return path;
}

借鉴自:https://www.cnblogs.com/fishtreeyu/archive/2010/12/27/1918076.html

博主说:

1
2
3
4
5
-- 备份数据库
backup database db_CSManage to disk='c:\backup.bak'
-- 还原数据库,必须先备份该数据库的日志文件到原先的备份文件中
backup log db_CSManage to disk='c:\backup.bak'
restore database db_CSManage from disk='c:\backup.bak'

但在还原数据库时报错误

似乎他少写了一句话,改成这样就好了

1
2
3
4
5
6
7
-- 备份数据库
backup database OA to disk='d:\backup.bak'

-- 备份该数据库的日志文件到原先的备份文件中
backup LOG OA to disk='d:\backup.bak' WITH NORECOVERY
--还原数据库,
restore database OA from disk='d:\backup.bak' WITH STATS = 10,RECOVERY;

结果:

好了,知道这个后直接在项目中执行该sql语句即可。

但是有个坑点,在项目开发中,数据库备份的在安装数据库的那台电脑上,并不在vs项目文件夹中,需下载回本地

MVC控制器备份数据库:

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
/// <summary>
/// 备份数据文件
/// </summary>
/// <param name="dbName"></param>
/// <param name="dbBackupName"></param>
/// <returns></returns>
public ActionResult Backuping(string dbName, string dbBackupName)
{
if (dbName == null || dbName.Length == 0)
return Json(new { msg = false, error = "输入的数据库不存在!" }, JsonRequestBehavior.AllowGet);

string path = ConfigurationManager.AppSettings["DbBackPath"] + dbName + "/" + DateTime.Now.ToString("yyyyMMdd");
//path = Server.MapPath(path);路径只能是服务器上的

//如果路径不存在就创建文件夹
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}

//判断文件是否已存在
string filePath = path + "/" + dbBackupName.Trim() + ".bak'";
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
}
string str = ConfigurationManager.ConnectionStrings[dbName].ToString();
using (SqlConnection con = new SqlConnection(str))
{

string strBacl = "backup database " + dbName + " to disk='" + filePath;

using (SqlCommand Cmd = new SqlCommand(strBacl, con))
{
con.Open();
if (Cmd.ExecuteNonQuery() != 0)
{
con.Close();
return Json(new { succeed = true, filePath = filePath, error = "备份数据库成功!" }, JsonRequestBehavior.AllowGet);
}
else
{
con.Close();
return Json(new { succeed = false, error = "备份数据库失败!" }, JsonRequestBehavior.AllowGet);
}
}
}
}

MVC控制器还原数据库:

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
/// <summary>
/// 数据库恢复操作
/// </summary>
/// <param name="dbName"></param>
/// <param name="fileUrlUpload"></param>
/// <returns></returns>
public ActionResult RecoverDatabase(string dbName, string fileUrlUpload)
{

if (dbName == null || dbName.Length == 0)
return Json(new { msg = false, error = "输入的数据库不存在!" }, JsonRequestBehavior.AllowGet);

string DateStr =ConfigurationManager.ConnectionStrings["master"].ConnectionString;

string DbFilePath = @"F:\DbBackup\tmm\20171122\tmm_ba.bak";//Session["DbFilePath"].ToString();

SqlConnection con = new SqlConnection(DateStr);
if (con.State == ConnectionState.Open)
{
con.Close();
}

SqlConnection conn = new SqlConnection(DateStr);
conn.Open();

string strSQL = "select spid from master..sysprocesses where dbid=db_id( '" + dbName + "') ";

SqlDataAdapter Da = new SqlDataAdapter(strSQL, conn);
DataTable spidTable = new DataTable();
Da.Fill(spidTable);

SqlCommand Cmd = new SqlCommand();
Cmd.CommandType = CommandType.Text;
Cmd.Connection = conn;

for (int iRow = 0; iRow <= spidTable.Rows.Count - 1; iRow++)
{
Cmd.CommandText = "kill " + spidTable.Rows[iRow][0].ToString(); //强行关闭用户进程
Cmd.ExecuteNonQuery();
}

//--------------------------------------------------------------------
SqlConnection sqlcon = new SqlConnection(DateStr);
sqlcon.Open();
string sql = "backup DATABASE " + dbName + " to disk='" + DbFilePath + "' restore database "+dbName+" from disk='" + DbFilePath + "'";
SqlCommand sqlCmd = new SqlCommand(sql, sqlcon);
sqlCmd.ExecuteNonQuery();
sqlCmd.Dispose();
sqlcon.Close();
sqlcon.Dispose();
return Json(new { succeed = false, error = "备份数据库失败!" }, JsonRequestBehavior.AllowGet);
}

先需要切换到系统数据库下,然后先删除与需还原数据库相关的进程,然后在还原之前得先把数据库日志备份到开始的备份文件中,然后才还原备份文件,要不然会出错的,链接字符串在config文件中

明天再贴效果图,

又遇到这个问题了,还好及时发现了。之前也遇见过这个问题,困惑了很久。

这次有经验了,一下就发现了存在的问题,自此记录一下,下次不在犯了。

转自:https://www.cnblogs.com/terryMe/p/6130309.html

小哥哥描述得很清楚:

如果jquery中,获取不到ajax返回值。

两个错误写法会导致这种情况:1.ajax未用同步 2.在ajax方法中直接return返回值。

下面列举了三种写法,如果想成功获取到返回值,参考第三种写法:

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
/**
* 方式:(1)同步调用 (2)在ajax函数中return值
* 结果:返回 1。未成功获取返回值
* 失败原因:ajax内部是一个或多个定义的函数,ajax中return返回值,
* 返回到ajax定义函数,而不是返回到ajax外层的函数checkAccount1()
*/
function checkAccount1(){
var result = 1;
$.ajax({
url : 'test.do',
type : "post",
data : {},
async : false,
success : function(data) {
return 2;
}
});
return result;
}

/**
* (1)异步调用 (2)在ajax对全局变量进行设值 (3)ajax函数外将变量return
* 结果:返回 1。未成功获取返回值
* 失败原因:ajax请求和后面的return result语句异步执行,
* 导致return result比result = 2先执行并返回result的值
*/
function checkAccount2(){
var result = 1;
$.ajax({
url : 'test.do',
type : "post",
data : {},
async : true,
success : function(data) {
result = 2;
}
});
return result;
}

/**
* (1)同步调用 (2)且在ajax对全局变量进行设值 (3)ajax函数外将变量return
* 结果:返回 2。成功获取返回值
* 成功原因:先执行result = 2;再往下执行return result;
*/
function checkAccount3(){
var result = 1;
$.ajax({
url : 'test.do',
type : "post",
data : {},
async : false,
success : function(data) {
result = 2;
}
});
return result;
}