SQL 查询在 C# 中执行,但没有 return 任何结果
SQL query executes in C# but it does not return any result
我需要用我的 C# 执行我的 SQL 服务器 xevent 创建并启动它,然后从文件中读取它的内容。查询执行正常,未显示任何错误,但未在 SQL 服务器中添加会话,因此未返回任何结果。我在 SSMS 上执行了查询,它使用 N' 将变量连接到查询,但是当我将相同的变量复制到 C# 时,它显示了不正确的语法。有帮助吗?
My C#:
public ActionResult CreateSession(string id)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
string dbName = id;
string path = @".\wwwroot\ExecutedQueries\"+dbName;
System.IO.Directory.CreateDirectory(path);
string fileName = dbName +DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") +".xel";
string pathString = System.IO.Path.Combine(path, fileName);
if (!System.IO.File.Exists(pathString))
{
System.IO.FileStream fs = System.IO.File.Create(pathString);
}
string sql="DECLARE @sql nvarchar(max) = N'CREATE EVENT SESSION " + dbName + "_Session1"
+ " ON SERVER" + "ADD EVENT sqlserver.sql_statement_completed"
+ "(ACTION(sqlserver.sql_text, sqlserver.tsql_stack, sqlserver.client_app_name," +
" sqlserver.client_hostname, sqlserver.username) WHERE(sqlserver.database_name =''" + dbName + "'')),"
+ "ADD EVENT sqlserver.sql_statement_starting(ACTION(sqlserver.sql_text, sqlserver.tsql_stack, sqlserver.client_app_name, sqlserver.client_hostname, sqlserver.username) WHERE(sqlserver.database_name =''" + dbName + "''))"
+ "ADD target package0.event_file( SET filename =''" + pathString + "'')"
+ "WITH(MAX_MEMORY = 4096 KB, EVENT_RETENTION_MODE = ALLOW_SINGLE_EVENT_LOSS,"
+ "MAX_DISPATCH_LATENCY = 30 SECONDS, MAX_EVENT_SIZE = 0 KB,"
+ " MEMORY_PARTITION_MODE = NONE, TRACK_CAUSALITY = OFF, STARTUP_STATE = OFF);";
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
return RedirectToAction("BeginSession", new { database = dbName });
}
public ActionResult BeginSession(string database)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
string sql = "DECLARE @sql nvarchar(max) = N'ALTER EVENT SESSION "+@database+"_Session1 ON SERVER STATE = START '";
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
return RedirectToAction("ExecutedQueries", new { dataBase = database });
}
public ActionResult ExecutedQueries(string dataBase)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
string path = @"./wwwroot/ExecutedQueries/" + dataBase
string fileName = dataBase + "*.xel";
string pathString = System.IO.Path.Combine(path, fileName);
try
{
byte[] readBuffer = System.IO.File.ReadAllBytes(pathString);
foreach (byte b in readBuffer)
{
Console.Write(b + " ");
}
Console.WriteLine();
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
return View();
}
这生成了可以执行的SQL:
string sql = "CREATE EVENT SESSION " + dbName + "_Session1" +
" ON SERVER" +
" ADD EVENT sqlserver.sql_statement_completed" +
"( ACTION(sqlserver.sql_text, sqlserver.tsql_stack, sqlserver.client_app_name," +
" sqlserver.client_hostname, sqlserver.username) WHERE(sqlserver.database_name ='" + dbName + "'))," +
"ADD EVENT sqlserver.sql_statement_starting( ACTION(sqlserver.sql_text, sqlserver.tsql_stack, sqlserver.client_app_name,sqlserver.client_hostname, sqlserver.username) WHERE(sqlserver.database_name ='" + dbName + "'))" +
"ADD target package0.event_file( SET filename ='" + pathString + "')" +
"WITH(MAX_MEMORY = 4096 KB, EVENT_RETENTION_MODE = ALLOW_SINGLE_EVENT_LOSS," +
"MAX_DISPATCH_LATENCY = 30 SECONDS, MAX_EVENT_SIZE = 0 KB," +
" MEMORY_PARTITION_MODE = NONE, TRACK_CAUSALITY = OFF, STARTUP_STATE = OFF);";
我更改了它,使其直接创建事件(而不是创建一个未执行的@sql 变量),并且在 ADD EVENT 之前缺少一个 space,它正在生成 SERVERADD 而不是 SERVER ADD。
我需要用我的 C# 执行我的 SQL 服务器 xevent 创建并启动它,然后从文件中读取它的内容。查询执行正常,未显示任何错误,但未在 SQL 服务器中添加会话,因此未返回任何结果。我在 SSMS 上执行了查询,它使用 N' 将变量连接到查询,但是当我将相同的变量复制到 C# 时,它显示了不正确的语法。有帮助吗?
My C#:
public ActionResult CreateSession(string id)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
string dbName = id;
string path = @".\wwwroot\ExecutedQueries\"+dbName;
System.IO.Directory.CreateDirectory(path);
string fileName = dbName +DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") +".xel";
string pathString = System.IO.Path.Combine(path, fileName);
if (!System.IO.File.Exists(pathString))
{
System.IO.FileStream fs = System.IO.File.Create(pathString);
}
string sql="DECLARE @sql nvarchar(max) = N'CREATE EVENT SESSION " + dbName + "_Session1"
+ " ON SERVER" + "ADD EVENT sqlserver.sql_statement_completed"
+ "(ACTION(sqlserver.sql_text, sqlserver.tsql_stack, sqlserver.client_app_name," +
" sqlserver.client_hostname, sqlserver.username) WHERE(sqlserver.database_name =''" + dbName + "'')),"
+ "ADD EVENT sqlserver.sql_statement_starting(ACTION(sqlserver.sql_text, sqlserver.tsql_stack, sqlserver.client_app_name, sqlserver.client_hostname, sqlserver.username) WHERE(sqlserver.database_name =''" + dbName + "''))"
+ "ADD target package0.event_file( SET filename =''" + pathString + "'')"
+ "WITH(MAX_MEMORY = 4096 KB, EVENT_RETENTION_MODE = ALLOW_SINGLE_EVENT_LOSS,"
+ "MAX_DISPATCH_LATENCY = 30 SECONDS, MAX_EVENT_SIZE = 0 KB,"
+ " MEMORY_PARTITION_MODE = NONE, TRACK_CAUSALITY = OFF, STARTUP_STATE = OFF);";
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
return RedirectToAction("BeginSession", new { database = dbName });
}
public ActionResult BeginSession(string database)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
string sql = "DECLARE @sql nvarchar(max) = N'ALTER EVENT SESSION "+@database+"_Session1 ON SERVER STATE = START '";
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
return RedirectToAction("ExecutedQueries", new { dataBase = database });
}
public ActionResult ExecutedQueries(string dataBase)
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(connectionString);
string path = @"./wwwroot/ExecutedQueries/" + dataBase
string fileName = dataBase + "*.xel";
string pathString = System.IO.Path.Combine(path, fileName);
try
{
byte[] readBuffer = System.IO.File.ReadAllBytes(pathString);
foreach (byte b in readBuffer)
{
Console.Write(b + " ");
}
Console.WriteLine();
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}
return View();
}
这生成了可以执行的SQL:
string sql = "CREATE EVENT SESSION " + dbName + "_Session1" +
" ON SERVER" +
" ADD EVENT sqlserver.sql_statement_completed" +
"( ACTION(sqlserver.sql_text, sqlserver.tsql_stack, sqlserver.client_app_name," +
" sqlserver.client_hostname, sqlserver.username) WHERE(sqlserver.database_name ='" + dbName + "'))," +
"ADD EVENT sqlserver.sql_statement_starting( ACTION(sqlserver.sql_text, sqlserver.tsql_stack, sqlserver.client_app_name,sqlserver.client_hostname, sqlserver.username) WHERE(sqlserver.database_name ='" + dbName + "'))" +
"ADD target package0.event_file( SET filename ='" + pathString + "')" +
"WITH(MAX_MEMORY = 4096 KB, EVENT_RETENTION_MODE = ALLOW_SINGLE_EVENT_LOSS," +
"MAX_DISPATCH_LATENCY = 30 SECONDS, MAX_EVENT_SIZE = 0 KB," +
" MEMORY_PARTITION_MODE = NONE, TRACK_CAUSALITY = OFF, STARTUP_STATE = OFF);";
我更改了它,使其直接创建事件(而不是创建一个未执行的@sql 变量),并且在 ADD EVENT 之前缺少一个 space,它正在生成 SERVERADD 而不是 SERVER ADD。