2016-09-26 25 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

可能達到每連接模式選擇最簡單的方法就是您的模式映射到用戶,並使用正確的用戶連接到數據庫。這意味着他們將自動查詢他們的默認模式。