I have a simple select query in a verbatim string in C#/.net core 2.1 that looks somewhat like this
public const string QUERY_HTML = @"SELECT col1, col2 from table1 JOIN table2 ON table1.id = table2.id WHERE data LIKE '%<span class=''myclass-ident''%' ORDER BY col1";
EDIT: Per commenters suggestion, I ran the profiler and the the query in the profiler is the same as the one in the code and the text visualizer in debug. In other words, the genericized query above does seem to be the one that gets run, at least that's what the profiler says.
The actual query isn't important - I genericized the column names, but its the same otherwise. If I run it in SQL Server Management Studio, it works just fine.
But when my code runs the query, it clobbers the database. Meaning the database is completely barebones. All users, tables, stored procedures and data (obviously if there are no tables) are gone.
If I run the query without the @
sign it works just fine.
I couldn't believe it at first - I thought there must be something else going on. I restored my local database and ran it again with the @
sign and it again reset the database to a bare/skeleton state. I also tried it on a different machine.
Obviously, I know to not use the @
sign in front of the query. I originally had it in there to escape some characters that have since been removed, so I no longer need the @
but it still leaves the question as to why would the @
cause the database to be clobbered?
SQL Server version is 2012 (v11.0.7001.0).
If it helps, here is the code that runs the query
using (var conn = new SqlConnection(Constants.LOCAL))
{
using (var cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = Constants.QUERY_HTML;
conn.Open();
DataTable dt = new DataTable();
try
{
using (var reader = cmd.ExecuteReader())
{
dt.Load(reader);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
if (dt.Rows.Count > 0)
{
result.payload = dt;
result.hasPayload = true;
result.code = 200;
}
return result;
}
}