【问题标题】:How to configure SQL Server 2014 Database using MVC 5如何使用 MVC 5 配置 SQL Server 2014 数据库
【发布时间】:2015-04-11 23:49:30
【问题描述】:

我正在尝试将我的 SQL Server 2014 数据库与我的 MVC 应用程序连接起来。

我的代码如下:

模型类Category.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GetImage.Models
{
    public class Category
    {

        public int CategoryID { get; set; }

        public string CategoryName { get; set; }

        public byte[] Picture { get; set; }
    }
}

我的上下文类CategoryContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace GetImage.Models
{
    public class CategoryContext
    {
        SqlConnection connection = null;
        SqlCommand command = null;
        SqlDataReader reader = null;
        string connectionString = ConfigurationManager.ConnectionStrings["Spencers"].ConnectionString;

        //Get Categories
        public List<Category> GetCategories()
        {
            List<Category> categories = null;
            try
            {
                using (connection = new SqlConnection(connectionString))
                {
                    using (command=new SqlCommand("uspGetCategories",connection))
                    {
                        connection.Open();
                        if (connection.State == ConnectionState.Open)
                        {
                            command.CommandType = CommandType.StoredProcedure;
                            reader = command.ExecuteReader();
                            categories = new List<Category>();
                            while (reader.Read())
                            {
                                categories.Add(
                                    new Category() { 
                                    CategoryID=int.Parse(reader["CategoryID"].ToString()),
                                    CategoryName=reader["CategoryName"].ToString(),
                                    Picture=reader["Picture"] as byte[]
                                    }
                                    );
                            }
                        }
                    }
                }
                return categories;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}

web.config 文件中:

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-GetImage-20150411072900.mdf;Initial Catalog=aspnet-GetImage-20150411072900;Integrated Security=True"
         providerName="System.Data.SqlClient" />
    <!--Own DB Connection-->
    <add name="Spencers" 
         connectionString="Data SourceVIKASHPC\vikash;Initial Catalog=Spencers;" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>

注意:

通过以上代码,我正在尝试连接我的 SQL Server 2014 数据库。

默认情况下,ASP.NET MVC 应用程序与localDB 连接。我不想联系localDB。相反,我想连接到我的 SQL Server 2014 数据库。

请帮我解决这个问题。

【问题讨论】:

    标签: asp.net-mvc-5 sql-server-2014


    【解决方案1】:

    确保您的连接字符串有效。我在数据源之后看到了一个丢失的=。此外,您可能需要提供身份验证详细信息

    connectionString="Data Source=VIKASHPC\vikash;Initial Catalog=Spencers;
                                     User Id=yourSqlUser;Password=yourSqlPassword"
    

    假设您的 SQL Server 实例名称是 VIKASHPC\vikash,它已启动并正在运行。还要确保 sql 用户可以访问您的数据库。(尝试从 SSMS 以该用户身份登录)

    检查https://www.connectionstrings.com/ 以查看有效的连接字符串格式。

    【讨论】:

    • 我没有用户名和密码。我正在使用 Windows 身份验证。
    • 更新您的连接字符串以反映这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多