I'm trying to replace my hardcoded SQL queries with LINQ expressions.
Here's the (simplified) code I want to replace:
List<string> sqlCommands = new List<string>
{
@"EXEC sp_attach_single_file_db @dbname='LocalDB', @physname=N'C:\dbfile.mdf'",
@"EXEC sp_addlinkedserver @server='NotLocalDB'"
};
SqlConnection conn = new SqlConnection(@"Server=.\SQLEXPRESS; Integrated Security=True");
conn.Open();
foreach (string commandString in sqlCommands)
{
var command = new SqlCommand(commandString, conn);
command.ExecuteNonQuery();
}
conn.Close();
I've replaced the sp_attach_single_file_db
command with this LINQ statement:
DBDataContext localDB = new
DBDataContext(@"Server=.\SQLEXPRESS; Database=LocalDB; Integrated Security=True");
localDB.CreateDatabase();
But I can't find an equivalent command for sp_addlinkedserver
.
Is there a way I can create a linked server for localDB
using LINQ?