3

I have got a console app that is connecting to a Sql Server and getting some values. I have the Schema in the Select(dbo):

        var ds = new DataTable("test");
        var connSqlRemoto = new SqlConnection("Server=myserverIP;Database=myDataBase;User Id=user;Password=pass;Integrated Security=False;Connection Timeout=60");
        connSqlRemoto.Open();
        var nombreBbdd = connSqlRemoto.Database;
        var daASqlRemoto = new SqlDataAdapter();
        var cmdSqlRemoto = new SqlCommand("SELECT * FROM " + nombreBbdd + ".dbo.myTable;", connSqlRemoto);
        cmdSqlRemoto.CommandTimeout = 1200;
        cmdSqlRemoto.Parameters.Clear();
        daASqlRemoto.SelectCommand = cmdSqlRemoto;
        daASqlRemoto.Fill(ds);

I want the Schema to be dynamic. Is it possible to pass the Schema in the connection string? Something like this is not working:

 Server=myserverIP;Database=myDataBase/dbo;User Id=user;Password=pass;Integrated Security=False;Connection Timeout=60

or

Server=myserverIP;Database=myDataBase.otherSchema;User Id=user;Password=pass;Integrated Security=False;Connection Timeout=60

Thanks.

3
  • No, this is done at the database User level, not in the connection string Commented Sep 26, 2016 at 9:28
  • 1
    I want teh Schema to be dynamic. So: Is it possible to pass the Schema in the connection string?..how can the schema be dynamic,when you pass in connection string Commented Sep 26, 2016 at 9:30
  • ok, thank you. @TheGameiswar Because it is an app that is receiving different connection strings. Commented Sep 26, 2016 at 9:31

2 Answers 2

2

Probably the easiest way to achieve a per-connection schema selection is to map your schema onto users, and connect to the database using the correct user. This will mean they will automatically query their default schema.

Sign up to request clarification or add additional context in comments.

Comments

1

No. You can not pass schema with connection string. But you can pass schema in sqlcommand like this.

var schema=".dbo." -- you can set it globally or can change dynamically
cmdSqlRemoto = new SqlCommand("SELECT * FROM " + nombreBbdd + schema + "myTable;", connSqlRemoto);

2 Comments

Yes, but I have got the connectionstring in a field of the database and i would want not to create another field...
In that case I suggest you to save schema along with database in same column. e.g instead of saving "dbname" save "dbname.dbo"

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.