2016-09-26 30 views
0

我有一个连接到Sql Server并获取一些值的控制台应用程序。我在选择(DBO)架构:在连接字符串中指定Sql Server架构C#

 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); 

我想德模式是动态的。所以: 是否有可能在连接字符串中传递模式? 这样的事情是不工作:

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

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

感谢。

+0

[不,这是在数据库用户级别完成的,而不是在连接字符串中](http://stackoverflow.com/a/3282716/1042848) –

+1

'我想要架构是动态的。所以:是否有可能在连接字符串中传递模式?'如果模式是动态的,当你传递连接字符串 – TheGameiswar

+0

好的时候,谢谢。 @TheGameiswar因为它是一个接收不同连接字符串的应用程序。 – Za7pi

回答

1

不可以。您不能使用连接字符串传递模式。但是你可以像这样在sqlcommand中传递模式。

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

是的,但我已经得到了数据库字段中的连接字符串,我不想创建另一个字段... – Za7pi

+0

在这种情况下,我建议您将模式与数据库一起保存在同一列中。例如,不要保存“dbname”保存“dbname.dbo” –

1

可能达到每连接模式选择最简单的方法就是您的模式映射到用户,并使用正确的用户连接到数据库。这意味着他们将自动查询他们的默认模式。