1

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Charles Clayton
  • 17,005
  • 11
  • 87
  • 120

2 Answers2

0
var connStringBuilder = new SqlConnectionStringBuilder();
connStringBuilder.DataSource = ".\SQLEXPRESS";
connStringBuilder.IntegratedSecurity = true;
connStringBuilder.InitialCatalog = "LocalDB";

SqlConnection conn = new SqlConnection(connStringBuilder)
Kiryl
  • 95
  • 4
-2

Try Initial Catalog instead of Database

and

Data Source instead of Server

Kiryl
  • 95
  • 4
  • 1
    I feel like that doesn't really answer my question, nor is it helpful because they're synonymous. See a) http://stackoverflow.com/a/12238552/2374028 and b) http://stackoverflow.com/a/15025693/2374028 – Charles Clayton Jul 22 '15 at 18:08