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?

0

2 Answers 2

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

SqlConnection conn = new SqlConnection(connStringBuilder)
Sign up to request clarification or add additional context in comments.

1 Comment

What does that have to do with adding a linked, non-local DB?
-2

Try Initial Catalog instead of Database

and

Data Source instead of Server

1 Comment

I feel like that doesn't really answer my question, nor is it helpful because they're synonymous. See a) stackoverflow.com/a/12238552/2374028 and b) stackoverflow.com/a/15025693/2374028

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.