为什么serverSelected为null?
问题描述:
ViewModel:
ViewModel :
string _serverSelected;
public string serverSelected
{
get { return _serverSelected; }
set
{
if (_serverSelected != value)
_serverSelected = value;
RaisePropertyChanged("serverSelected");
}
}
public BackUpViewModel()
{
BackUpContext servObj = new BackUpContext();
_DBNameList = servObj.GetDBNames(serverSelected);
}
DAL:
DAL :
public List<string> GetDBNames(string sqlServerName)
{
List<string> lstDBName = new List<string>();
SqlConnectionStringBuilder connection = new SqlConnectionStringBuilder();
connection.DataSource = sqlServerName;
connection.IntegratedSecurity = true;
String strCon = connection.ToString();
SqlConnection sqlConn = new SqlConnection(strCon);
sqlConn.Open();
SqlCommand cmd = new SqlCommand("select * from sys.databases", sqlConn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
lstDBName.Add(dr[0].ToString());
}
sqlConn.Close();
return lstDBName;
}
AppConfig:
AppConfig :
<configuration>
<connectionStrings>
<add name="CostAllocationEntities2" connectionString="Data Source=pc210090\SQLExpress;Persist Security Info=True;User ID=sa;Password=password1$" providerName="System.Data.SqlClient"/>
</connectionStrings>
<appSettings>
<add key="ServerName1" value="PC210090\SQLEXPRESS"/>
<add key="ServerName2" value="s2"/>
<add key="ServerName3" value="s3"/>
<add key="BackupDestinationPath" value="D:\BackUpDatabase\"/>
<add key="RestoreDestinationPath" value="D:\RestoreDatabase\"/>
</appSettings>
</configuration>
Quote:
_DBNameList = servObj.GetDBNames(serverSelected);这个serverSelected总是传递为null,这不允许我的代码构建,因此我无法从服务器列表中选择服务器。
_DBNameList = servObj.GetDBNames(serverSelected); this serverSelected is always passing as null which is not allowing my code to build and i am not able to select server from server list because of this.
答
providerName = System.Data.SqlClient / >
< / connectionStrings >
< appSettings >
< add key = 丝氨酸verName1 value = PC210090 \ SQLEXPRESS / >
< add key = ServerName2 value = s2 / >
< add key = ServerName3 值 = s3 / >
< add key = BackupDestinationPath 值 = D:\ BackUpDatabase \ / >
< add key = RestoreDestinationPath value = D: \恢复数据库\ / >
< / appSettings >
< / configuration >
" providerName="System.Data.SqlClient"/> </connectionStrings> <appSettings> <add key="ServerName1" value="PC210090\SQLEXPRESS"/> <add key="ServerName2" value="s2"/> <add key="ServerName3" value="s3"/> <add key="BackupDestinationPath" value="D:\BackUpDatabase\"/> <add key="RestoreDestinationPath" value="D:\RestoreDatabase\"/> </appSettings> </configuration>
Quote:
_DBNameList = servObj.GetDBNames(serverSelected);这个serverSelected总是作为null传递,这不允许我的代码构建,因此我无法从服务器列表中选择服务器。
_DBNameList = servObj.GetDBNames(serverSelected); this serverSelected is always passing as null which is not allowing my code to build and i am not able to select server from server list because of this.
您好,在您的代码中,你没有提到数据库名称,只是这样提,
Hi, In your code, you did not mention the database name, just mention it like this,
connection.InitialCatalog = "YourDatabaseNameHere";