Linux上访问SQL Server数据库

时间:2022-05-07
本文章向大家介绍Linux上访问SQL Server数据库,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

.NET跨平台之旅:升级至ASP.NET 5 RC1,Linux上访问SQL Server数据库

今天微软正式发布了ASP.NET 5 RC1(详见Announcing ASP.NET 5 Release Candidate 1),.NET跨平台迈出了关键一步。

紧跟这次RC1的发布,我们成功地将运行在Linux上的示例站点(http://about.cnblogs.com)升级到了ASP.NET 5 RC1,并且增加了数据库访问功能——基于Entity Framework 7 RC1访问SQL Server数据库。

示例站点页面左侧的导航是从数据库读取数据动态加载的,数据库服务器用的是阿里云RDS(注:创建数据库时需要将支持的字符集设置为SQL_Latin1_General_CP1_CS_AS,这是针对SqlClient中一个bug的临时解决方法)。数据库表是通过EF迁移功能生成的,所用命令如下:

dnx ef migrations add FirstMigration
dnx ef database update

数据库连接字符串是从config.json中读取的。

后端Web服务器用的是kestrel,前端Web服务器用的是阿里云负载均衡,使用中发现一个很奇怪的问题:浏览器直接访问kestrel,速度飞快;而访问阿里云负载均衡,页面虽然显示出来,但页面一直牌加载状态,长达1分钟。

怀疑是阿里云负载均衡与kestrel在TCP通信上存在某些问题,这个问题暂时没有找到解决方法(更新:这是kestrel的一个bug,详见 Don't wait to consume the entire request body for Connection: close requests )。

SQL Server数据库终于能跨平台访问了,接下来就看kestrel的稳定性了。如果kestrel稳定,我们就开始将一些实际使用的小站点迁移至ASP.NET 5,并部署在Linux服务器上。

下面分享一下这个示例ASP.NET 5站点的主要代码。

文件结构:

.
├── config.json
├── Controllers
│   ├── AboutController.cs
│   └── HomeController.cs
├── Data
│   ├── EfDbContext.cs
│   ├── ITabNavRepository.cs
│   └── TabNavRepository.cs
├── Extensions
│   └── HtmlHelperExtensions.cs
├── Models
│   └── TabNav.cs
├── project.json
├── project.lock.json
├── Startup.cs
├── Views
│   ├── About
│   │   ├── Ad.cshtml
│   │   ├── Contact.cshtml
│   │   ├── Intro.cshtml
│   │   └── Job.cshtml
│   ├── Shared
│   │   └── _Layout.cshtml
│   └── _ViewStart.cshtml
└── wwwroot

project.json文件的内容:

{    "webroot": "wwwroot",    "exclude": ["wwwroot"],    "commands":{        "kestrel": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://*:8001",        "ef": "EntityFramework.Commands"
    },    "dependencies":{        "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",        "Microsoft.AspNet.Mvc": "6.0.0-*",        "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",        "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",        "EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",        "EntityFramework.Commands": "7.0.0-rc1-final",        "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",        "System.Runtime.Serialization.Primitives": "4.0.10-*",        "System.Net.Security":"4.0.0-*"
    },    "frameworks":{        "dnxcore50": {},
    }
}

Startup.cs中的代码:

using System;using System.Linq;using Microsoft.AspNet.Builder;using Microsoft.Extensions.DependencyInjection;using Microsoft.Data.Entity;using CNBlogs.AboutUs.Data;using Microsoft.Dnx.Runtime;using Microsoft.Extensions.PlatformAbstractions;using Microsoft.Extensions.Configuration;using System.Data.SqlClient;using Microsoft.Extensions.Logging;namespace CNBlogs.AboutUs.Web
{    public class Startup
    {        public Startup(IApplicationEnvironment appEnv)
        {
            IConfigurationBuilder builder = new ConfigurationBuilder()
                          .SetBasePath(appEnv.ApplicationBasePath)
                          .AddJsonFile("config.json", false);
            Configuration = builder.Build();
        }        public IConfiguration Configuration { get; set; }        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();
            app.UseDeveloperExceptionPage();
            app.UseMvcWithDefaultRoute();
            app.UseStaticFiles();
            app.UseRuntimeInfoPage();
        }        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<EfDbContext>(options =>
                {
                    options.UseSqlServer(Configuration["data:ConnectionString"]);
                });

            services.AddTransient<ITabNavRepository, TabNavRepository>();
        }
    }
}