C# Visual Studio与SQL Server的通信--服务器错误

C# Visual Studio与SQL Server的通信--服务器错误,c#,asp.net,sql-server,vb.net,visual-studio,C#,Asp.net,Sql Server,Vb.net,Visual Studio,我在Visual Studio与SQL Server连接时遇到问题。。。 根据此代码: public ActionResult SearchByRestaurant() { User objUser = new User(); if (Session["UserId"] != null) { int id = Convert.ToInt32(Session["UserId"]); objUser = objContext.ContextU

我在Visual Studio与SQL Server连接时遇到问题。。。 根据此代码:

public ActionResult SearchByRestaurant()
{
    User objUser = new User();

    if (Session["UserId"] != null)
    {
        int id = Convert.ToInt32(Session["UserId"]);
        objUser = objContext.ContextUser.Find(id);
    }

    objUser.SearchResult = new List<SearchResult>();
    objUser.PreferencesList = objContext.ContextPreferences.Where(s => s.Status == 1).ToList()
    objUser.RestaurantListAll = objContext.ContextRestaurant.Where(s => s.Status == 1).ToList();

    if (objUser.SelectedPreferences == null)
        objUser.SelectedPreferences = new int[] { };

    return View(objUser);
}
objUser.PreferencesList = objContext.ContextPreferences.Where(s => s.Status == 1).ToList()
<configSections> <section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
EntityFramework, Version=6.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
</configSections> <connectionStrings> <add name="ContextClass" 
connectionString="Data Source=(local);Persist Security 
Info=True;database=db_FoodAllergy;User ID=sa;Password=saa" 
providerName="System.Data.SqlClient" /> </connectionStrings>
例外情况:

EntityFramework.dll中的“System.Data.SqlClient.SqlException”

其他信息:用户“sa”登录失败

我已尝试进入设置以添加“sa”用户作为连接,但它不起作用。 更多详细信息:

public ActionResult SearchByRestaurant()
{
    User objUser = new User();

    if (Session["UserId"] != null)
    {
        int id = Convert.ToInt32(Session["UserId"]);
        objUser = objContext.ContextUser.Find(id);
    }

    objUser.SearchResult = new List<SearchResult>();
    objUser.PreferencesList = objContext.ContextPreferences.Where(s => s.Status == 1).ToList()
    objUser.RestaurantListAll = objContext.ContextRestaurant.Where(s => s.Status == 1).ToList();

    if (objUser.SelectedPreferences == null)
        objUser.SelectedPreferences = new int[] { };

    return View(objUser);
}
objUser.PreferencesList = objContext.ContextPreferences.Where(s => s.Status == 1).ToList()
<configSections> <section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
EntityFramework, Version=6.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
</configSections> <connectionStrings> <add name="ContextClass" 
connectionString="Data Source=(local);Persist Security 
Info=True;database=db_FoodAllergy;User ID=sa;Password=saa" 
providerName="System.Data.SqlClient" /> </connectionStrings>
  • 使用Visual Studio ASP.Net框架
  • 用C写#
  • 使用已在VS中设置的基本SQL Server数据库
我真的非常感谢您能给予我的任何帮助,如果您愿意进一步帮助我,请给我留言。谢谢大家!

我尝试启用“sa”帐户,现在收到此错误:

public ActionResult SearchByRestaurant()
{
    User objUser = new User();

    if (Session["UserId"] != null)
    {
        int id = Convert.ToInt32(Session["UserId"]);
        objUser = objContext.ContextUser.Find(id);
    }

    objUser.SearchResult = new List<SearchResult>();
    objUser.PreferencesList = objContext.ContextPreferences.Where(s => s.Status == 1).ToList()
    objUser.RestaurantListAll = objContext.ContextRestaurant.Where(s => s.Status == 1).ToList();

    if (objUser.SelectedPreferences == null)
        objUser.SelectedPreferences = new int[] { };

    return View(objUser);
}
objUser.PreferencesList = objContext.ContextPreferences.Where(s => s.Status == 1).ToList()
<configSections> <section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
EntityFramework, Version=6.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
</configSections> <connectionStrings> <add name="ContextClass" 
connectionString="Data Source=(local);Persist Security 
Info=True;database=db_FoodAllergy;User ID=sa;Password=saa" 
providerName="System.Data.SqlClient" /> </connectionStrings>
EntityFramework.dll中的“System.InvalidOperationException”

其他信息:此操作需要连接到“主”数据库。无法创建到“主”数据库的连接,因为原始数据库连接已打开,并且凭据已从连接字符串中删除。提供未打开的连接

连接字符串:

public ActionResult SearchByRestaurant()
{
    User objUser = new User();

    if (Session["UserId"] != null)
    {
        int id = Convert.ToInt32(Session["UserId"]);
        objUser = objContext.ContextUser.Find(id);
    }

    objUser.SearchResult = new List<SearchResult>();
    objUser.PreferencesList = objContext.ContextPreferences.Where(s => s.Status == 1).ToList()
    objUser.RestaurantListAll = objContext.ContextRestaurant.Where(s => s.Status == 1).ToList();

    if (objUser.SelectedPreferences == null)
        objUser.SelectedPreferences = new int[] { };

    return View(objUser);
}
objUser.PreferencesList = objContext.ContextPreferences.Where(s => s.Status == 1).ToList()
<configSections> <section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
EntityFramework, Version=6.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
</configSections> <connectionStrings> <add name="ContextClass" 
connectionString="Data Source=(local);Persist Security 
Info=True;database=db_FoodAllergy;User ID=sa;Password=saa" 
providerName="System.Data.SqlClient" /> </connectionStrings>

您不必在本地sql server上提供登录凭据。所以试着使用这个连接字符串。这对你会有用的

Data Source=(local);database=db_FoodAllergy;Integrated Security=true; providerName="System.Data.SqlClient

如果我没记错的话,sa将是数据库的管理员