Cấp bậc tác giả:

TRAINING

[ASP.NET MVC]Phần 7: Tìm hiểu về các chuỗi kết nối Cơ sở dữ liệu(connection strings)

Được viết bởi webmaster ngày 09/11/2018 lúc 11:11 PM
Sau khi tạo được mô hình (Model) ở các bài trước, bạn nên tìm hiểu một chút về chuỗi kết nối database trong ASP.NET MVC
  • 0
  • 6623

[ASP.NET MVC]Phần 7: Tìm hiểu về các chuỗi kết nối Cơ sở dữ liệu(connection strings)

Sau khi tạo được mô hình (Model) ở các bài trước, bạn nên tìm hiểu một chút về chuỗi kết nối database trong ASP.NET MVC. Để xem nội dung chuỗi kết nối, bạn có thể mở tập tin Web.config trong dự án và tìm chỗ có thẻ connectionStrings. Nội dung một tập tin Web.config như sau:

<?xml version="1.0" encoding="utf-8"?>

<!--

  For more information on how to configure your ASP.NET application, please visit

  https://go.microsoft.com/fwlink/?LinkId=301880

  -->

<configuration>

  <configSections>

    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  </configSections>

  <appSettings>

    <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" />

  </appSettings>

  <system.web>

    <compilation debug="true" targetFramework="4.6" />

    <httpRuntime targetFramework="4.6" />

    <httpModules>

      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />

    </httpModules>

  </system.web>

  <runtime>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

      <dependentAssembly>

        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />

        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />

      </dependentAssembly>

      <dependentAssembly>

        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />

        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />

      </dependentAssembly>

      <dependentAssembly>

        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />

        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />

      </dependentAssembly>

      <dependentAssembly>

        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />

        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />

      </dependentAssembly>

      <dependentAssembly>

        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />

        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />

      </dependentAssembly>

      <dependentAssembly>

        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />

        <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />

      </dependentAssembly>

    </assemblyBinding>

  </runtime>

  <system.webServer>

    <validation validateIntegratedModeConfiguration="false" />

    <modules>

      <remove name="ApplicationInsightsWebTracking" />

<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />

    </modules>

  </system.webServer>

  <system.codedom>

    <compilers>

<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />

<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />

    </compilers>

  </system.codedom>

  <connectionStrings>

    <add name="TutorialMVCEntities" connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DOTNETGROUP;initial catalog=TutorialMVC;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

  </connectionStrings>

  <entityFramework>

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />

    <providers>

      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />

    </providers>

  </entityFramework>

</configuration>

Đoạn có chứa các chuỗi kết nối bạn cần tìm:

<connectionStrings><add name="TutorialMVCEntities" connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DOTNETGROUP;initial catalog=TutorialMVC;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

  </connectionStrings>

Bạn phải chú ý nhiều hơn chuỗi kết nối TutorialMVCEntities. Đây chính là chuỗi kết nối mà dự án Web hiểu được database từ SQL Server theo kiểu MVC. 
Ba tập tin Models.Model.csdl, Models.Model.ssdl và Models.Model.msl là các tập tin cấu hình MVC.  Theo đó, bạn đang dùng server SQL cài mặc định của máy bạn đang dùng (data source=DOTNETGROUP)và tên database là initial catalog=TutorialMVC với chế độ tích hợp bảo mật (integrated security=True), không cần tài khoản đăng nhập.
Bạn phải ghi đúng thông số chuỗi kết nối này và khi triển khai thực tế bạn phải thay đổi thông tin. Ví dụ, chuỗi kết nối triển khai trên server có IP database là 192.168.1.1 và có tài khoản đăng nhập User là “user” và Pass là “pass” thì bạn thay đổi chuỗi kết nối như sau:
<add name="TutorialMVCEntities" connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.1.1, 1433;initial catalog=TutorialMVC;User Id=user;Password=pass; integrated security=False;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
Ngoài cách trên ra, bạn còn có thể tham khảo các kiểu kết nối khác ở Các chuỗi kết nối cơ sở dữ liệu
Phần tiếp theo chúng ta sẽ truy cập mô hình dữ liệu từ controller

Nguồn bài viết: DOTNET.VN

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML