目前MVC项目。以前的开发人员数据库连接的应用程序设置现在我改为Web配置
我已经更改了代码,就像这个appconfig到webconfig一样
<appSettings>
<add key="TickPortalSql" value="Server=1.34.34.4.;Database=montage;User ID=Montage;Password=pwd;Trusted_Connection=False;" />
</appSettings>
此代码更改为web config
<connectionStrings>
<add key="TickPortalSql" value="Server=1.34.34.4.;Database=montage;User ID=Montage;Password=pwd;Trusted_Connection=False;" />
</connectionStrings>
以前的开发者使用AppSettingsReader
public abstract class HomeSqlDBRepository
{
public string ConnectionString { get; set; } = AppSettingsReader.ReadString("thenna");
const int MaxItemCount = 1000;
public enum SqlExecuteType
{
Scalar, NonQuery, Reader
}
public async Task<string> ExecuteSqlAsync(string procname, SqlExecuteType executiontype, IDictionary<string, string> parameters)
{
string ret = "";
using (SqlConnection sqlConn = new SqlConnection(ConnectionString))
{
using (SqlCommand sqlCmd = new SqlCommand(procname, sqlConn))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
foreach (KeyValuePair<string, string> kvp in parameters)
{
sqlCmd.Parameters.AddWithValue(kvp.Key, kvp.Value);
}
sqlConn.Open();
if (executiontype == SqlExecuteType.Scalar)
ret = (await sqlCmd.ExecuteScalarAsync()).ToString();
else if (executiontype == SqlExecuteType.NonQuery)
ret = (await sqlCmd.ExecuteNonQueryAsync()).ToString();
sqlConn.Close();
}
}
return ret;
}
但我在此处获得异常错误代码
using Microsoft.Azure;
public static class AppSettingsReader
{
public static string ReadString(string key)
{
try
{
return CloudConfigurationManager.GetSetting(key).ToString();
}
catch(System.Exception ex)
{
throw ex;//error
}
}
public static int ReadInt(string key)
{
try
{
return int.Parse(CloudConfigurationManager.GetSetting(key).ToString());
}
catch (System.Exception ex)
{
throw ex;
}
}
public static decimal ReadDecimal(string key)
{
try
{
return decimal.Parse(CloudConfigurationManager.GetSetting(key).ToString());
}
catch (System.Exception ex)
{
throw ex;
}
}
public static bool ReadBool(string key)
{
try
{
return bool.Parse(CloudConfigurationManager.GetSetting(key).ToString());
}
catch (System.Exception ex)
{
throw ex;
}
}
}
任何人都告诉如何更改azure app设置读取db to web config db read?我累了很多方法得错误..
答案 0 :(得分:1)
将web.config更改为:
<connectionStrings>
<add name="TickPortalSql"
value="Server=1.34.34.4.;Database=montage;UserID=Montage;Password=pwd;Trusted_Connection=False;" />
</connectionStrings>
并访问字符串
var connectionString = ConfigurationManager.ConnectionStrings["TickPortalSql"].ConnectionString;