我有MVC5工作。我创建了本地数据库连接本地sql服务器后,我创建了门户网站其工作服务器数据库连接,而不是连接数据库。 服务器数据库连接使用
Appseetings.config
<?xml version="1.0"?>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="DefaultMembershipName" value="UserName" />
<add key="SmtpServer" value="relay-hosting.secureserver.net" />
<add key="SmtpPort" value="25" />
<add key="UserId" value="thenna41@gmail.com" />
<add key="Password" value="pixel@1990" />
<add key="DefaultTheme" value="Theme1"/>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<!--<add key="thenna" value="server=121.11.1.2;database=montage;user id=montage;password=pass;trusted_connection=false;"/>-->
<!--<add key="thenna" value="server=DESKTOP-QD6A981\SQLEXPRESS;user id=montage;password=montage;trusted_connection=false;" />-->
<add key="thenna" value="server=DESKTOP-QD6A981\SQLEXPRESS;trusted_connection=true;" />
<add key="CustomAuthentication.LoginUrl" value="/Account/LogOn" />
<add key="CustomAuthentication.Cookie.Name" value=".CUSTOM_AUTH" />
<add key="CustomAuthentication.Cookie.Expiration" value="1" />
<add key="CustomAuthentication.Cookie.Timeout" value="20" />
</appSettings>
我已将本地数据库配置称为无法正常工作
key="thenna"
- &gt;用于从数据库连接中读取字符串
Appsettings Reader用于读取thenna键值
public class HomeSqlRepository : Repository
{
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;
}
public T GetItem<T>(string procname, IDictionary<string, object> parameters)
{
procname.ThrowIfNull();
using (IDbConnection db = new SqlConnection(ConnectionString))
{
var result= db.Query<T>(sql: procname, param: BuildQueryParameter(parameters), commandType: CommandType.StoredProcedure);
return result.FirstOrDefault();
}
}
应用程序设置使用cloudconfirmationmanager的阅读器 使用以下代码
public static class AppSettingsReader
{
public static string ReadString(string key)
{
try
{
return CloudConfigurationManager.GetSetting(key).ToString();
}
catch(System.Exception ex)
{
throw ex;
}
}
public static int ReadInt(string key)
{
try
{
return int.Parse(CloudConfigurationManager.GetSetting(key).ToString());
}
catch (System.Exception ex)
{
throw ex;
}
}
}
在哪里读取值我创建了db本地但不起作用但是当连接服务器时它正在工作。这里有什么错误?提前谢谢
答案 0 :(得分:0)
将您的连接字符串更改为
<add key="thenna" value="Data Source=DESKTOP-QD6A981\SQLEXPRESS;Integrated Security=True" />
如果这不起作用,请分享您遇到的错误。