Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6584805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:34:59+00:00 2026-05-25T16:34:59+00:00

I have stumbled across a problem and I can’t think of a nice, SAFE

  • 0

I have stumbled across a problem and I can’t think of a nice, SAFE way of completing it. I have a wrapper class around the “Sql.Helper” class (Microsoft.ApplicationBlocks.Data) and I am using Parameters everywhere.

I am trying to change the current DB in use for C#

USE @DBName

That code can’t be parametarised, and thus everywhere I read suggests running that as dynamic SQL (shudder) like so

DECLARE @SQL_SCRIPT NVARCHAR(100); 
SET @SQL_SCRIPT = REPLACE('USE {DBName};', '{DBName}', @DBName); 
exec sp_executesql @SQL_SCRIPT;

However, outside of this “sp_executesql” the DB remains unchanged. I then thought about appending this to the front of all the SQL calls, however this will not help.

When running this, the profiler tells me the code is finally run as:

exec sp_executesql N'DECLARE @SQL_SCRIPT NVARCHAR(100); SET @SQL_SCRIPT = REPLACE(''USE {DBName};'', ''{DBName}'', @DBName); exec sp_executesql @SQL_SCRIPT;',N'@DBName nvarchar(100)',@DBName=N'MyDatabaseName'

it is contained in yet another “exec sp_executesql” by default 🙁

Currently, and I know this isn’t the best method, but I am doing the following at the start of each call.

commandText = "USE " + this._currentDB + ";" + commandText;

Does anyone know a proper way of selecting a DB for this solution?

p.s. this._currentDB is SQL escaped (as best as I can)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:35:00+00:00Added an answer on May 25, 2026 at 4:35 pm

    You can change the database in your connection with the SqlConnection.Database property.

    EDIT: You have to use the SqlConnection.ChangeDatabase method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have finally stumbled across a problem that I can't already find the answer
I'm playing around with ICS and Widgets and have stumbled across a problem I
I have been playing about with canvas, but have stumbled across a problem. When
I'm quite new to hibernate and have stumbled on this problem, which I can't
I have stumbled across a problem where I am attempting to read in a
I've stumbled across a problem way beyond my area of expertise, and I don't
I'm trying out ASP.NET MVC routing and have of course stumbled across a problem.
I am learning about LLDP protocol and have stumbled across terms chassis ID and
I've been practicing for an upcoming programming competition and I have stumbled across a
I am looking for Java-based Less CSS compilers. On Github, I have stumbled across

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.