forked from RowTeam/SharpSQLTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecOptions.cs
More file actions
51 lines (47 loc) · 1.6 KB
/
ExecOptions.cs
File metadata and controls
51 lines (47 loc) · 1.6 KB
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace SharpSQLTools.FunModule
{
class ExecOptions
{
SqlConnection Conn;
Setting setting;
public ExecOptions(SqlConnection Conn, Setting setting)
{
this.Conn = Conn;
this.setting = setting;
}
/// <summary>
/// xp_cmdshell 执行命令
/// </summary>
/// <param name="Command">命令</param>
public void xp_cmdshell(String Command)
{
var sqlstr = $@"exec master..xp_cmdshell '{Command}'";
Console.WriteLine(Batch.RemoteExec(Conn, sqlstr, true));
}
/// <summary>
/// sp_cmdshell 执行命令
/// </summary>
/// <param name="Command">命令</param>
public void sp_cmdshell(String Command)
{
if (setting.Check_configuration("Ole Automation Procedures", 0))
{
if (setting.Enable_ola()) return;
}
var sqlstr = String.Format(@"
declare @shell int,@exec int,@text int,@str varchar(8000);
exec sp_oacreate 'wscript.shell',@shell output
exec sp_oamethod @shell,'exec',@exec output,'c:\windows\system32\cmd.exe /c {0}'
exec sp_oamethod @exec, 'StdOut', @text out;
exec sp_oamethod @text, 'ReadAll', @str out
select @str", Command);
Console.WriteLine(Batch.RemoteExec(Conn, sqlstr, true));
}
}
}