我有一个用 C#/ASP.NET 编写的 Web 应用程序,它使用类库来获取、插入和更新信息。 这是使用 2 种不同语言运行的应用程序:英语和法语。
用户登录后,我可以检测到他的文化(已存储在数据库中)并通过检索正确的资源文件正确显示法语/英语标签。
在类库 (.dll) 中,我为法语和英语版本添加了 2 个资源文件,以根据用户文化返回错误消息。
所以我的问题是当我尝试更新用户配置文件并且文化是法语时,“地址”字段为空,无论用户文化如何,系统都会不断返回英文消息
这是我的类库 (.dll) 的快照
public string Address1
{
get { return _Address1; }
set
{
if (string.IsNullOrEmpty(value))
{
// Error! The field « Address » is mandatory!");
throw new Exception(Resources.CTenant_Messages.Address1_Missing.ToString());
}
else if (value.ToString().Trim().Length > LibNamtek.Public.CConstant.LENGTH_300)
{
// Error! The total length for « Address » exceeds " + LibNamtek.Public.CConstant.LENGTH_300.ToString() + " caracters.");
throw new Exception(Resources.CTenant_Messages.Address1_Length.ToString().Replace("[LENGTH]", LibNamtek.Public.CConstant.LENGTH_300.ToString()));
}
else
{
_Address1 = value;
}
}
}
public DataSet UpdateUser()
{
string v_connection_string = CConnection.GetDBConnection();
SqlConnection v_connection = new SqlConnection(v_connection_string);
try
{
// Open Connection
v_connection.Open();
SqlCommand cmd = new SqlCommand("pr_update_user", v_connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@p_user_id", SqlDbType.Int);
cmd.Parameters["@p_user_id"].Value = this.UserId;
cmd.Parameters.Add("@p_name", SqlDbType.NVarChar);
cmd.Parameters["@p_name"].Size = LibNamtek.Public.CConstant.LENGTH_300;
cmd.Parameters["@p_name"].Value = this.Name;
cmd.Parameters.Add("@p_address1", SqlDbType.NVarChar);
cmd.Parameters["@p_address1"].Size = LibNamtek.Public.CConstant.LENGTH_300;
cmd.Parameters["@p_address1"].Value = this.Address1;;
cmd.Parameters.Add("@p_address2", SqlDbType.NVarChar);
cmd.Parameters["@p_address2"].Size = LibNamtek.Public.CConstant.LENGTH_300;
cmd.Parameters["@p_address2"].Value = (this.Address2 == LibNamtek.Public.CConstant.DEFAULT_STRING ? DBNull.Value : (object)this.Address2);
cmd.Parameters.Add("@p_city", SqlDbType.NVarChar);
cmd.Parameters["@p_city"].Size = LibNamtek.Public.CConstant.LENGTH_300;
cmd.Parameters["@p_city"].Value = this.City;
cmd.Parameters.Add("@p_province", SqlDbType.NVarChar);
cmd.Parameters["@p_province"].Size = LibNamtek.Public.CConstant.LENGTH_300;
cmd.Parameters["@p_province"].Value = this.Province;
cmd.Parameters.Add("@p_country_id", SqlDbType.Int);
cmd.Parameters["@p_country_id"].Value = this.CountryId;
cmd.Parameters.Add("@p_postal_code", SqlDbType.NVarChar);
cmd.Parameters["@p_postal_code"].Size = LibNamtek.Public.CConstant.LENGTH_30;
cmd.Parameters["@p_postal_code"].Value = this.PostalCode;
cmd.Parameters.Add("@p_phone1", SqlDbType.NVarChar);
cmd.Parameters["@p_phone1"].Size = LibNamtek.Public.CConstant.LENGTH_30;
cmd.Parameters["@p_phone1"].Value = this.Phone1;
cmd.Parameters.Add("@p_fax", SqlDbType.NVarChar);
cmd.Parameters["@p_fax"].Size = LibNamtek.Public.CConstant.LENGTH_30;
cmd.Parameters["@p_fax"].Value = (this.Fax == LibNamtek.Public.CConstant.DEFAULT_STRING ? DBNull.Value : (object)this.Fax);
cmd.Parameters.Add("@p_email", SqlDbType.NVarChar);
cmd.Parameters["@p_email"].Size = LibNamtek.Public.CConstant.LENGTH_300;
cmd.Parameters["@p_email"].Value = this.Email;
cmd.Parameters.Add("@p_memo", SqlDbType.NVarChar);
cmd.Parameters["@p_memo"].Value = (this.Memo == LibNamtek.Public.CConstant.DEFAULT_STRING ? DBNull.Value : (object)this.Memo);
cmd.Parameters.Add("@p_system_date", SqlDbType.DateTime);
cmd.Parameters["@p_system_date"].Value = this.SystemDate;
cmd.Parameters.Add("@p_system_user", SqlDbType.NVarChar);
cmd.Parameters["@p_system_user"].Size = LibNamtek.Public.CConstant.LENGTH_30;
cmd.Parameters["@p_system_user"].Value = this.SystemUser;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet("UpdateTenant");
da.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (v_connection != null)
{
v_connection.Close();
}
}
}
最佳答案
您需要根据用户的文化来设置线程的文化:
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureString);
其中 cultureString
是从您的数据库读取的“en-GB”或“fr-FR”。
您应该只需要在创建类或更改文化时执行此操作。然后代码应该选择正确的资源文件
关于c# - 如何从类库中获取正确的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16363091/