首页 > 数据库 >学习011-08-15 Connect an XAF Application to a Database Provider(将 XAF 应用程序连接到数据库提供程序)

学习011-08-15 Connect an XAF Application to a Database Provider(将 XAF 应用程序连接到数据库提供程序)

时间:2024-11-04 14:17:56浏览次数:3  
标签:15 string database 应用程序 Application connection 字符串 连接 XAF

Connect an XAF Application to a Database Provider(将 XAF 应用程序连接到数据库提供程序)

When an application runs for the first time, its database is created automatically. During the following runs in a debug mode, the application connects to the same database and updates it as required. When the application runs in a release mode, the database is not updated by default. For details, refer to the Create and Update the Application’s Database topic. If you ever need to have your database recreated, just drop it from the database server or remove the file, and it will be recreated automatically. By default, an XAF solution is configured to use an instance of the Microsoft SQL Server Express LocalDB on the local system, accessed via the integrated security. A database will be created on that server under the name of the solution you created. However, XPO and EF Core, which serve as an Object-Relational Mapping layer, support over a dozen database management systems. So you can change the default connection. To do this, you need to specify the required connection setting, and reference the required database provider connector assembly. This topic describes several approaches that allow you to connect an XAF application to the required database provider.
当应用程序首次运行时,它的数据库会自动创建。在以下以调试模式运行期间,应用程序连接到同一数据库并根据需要进行更新。当应用程序以发布模式运行时,默认情况下不会更新数据库。有关详细信息,请参阅创建和更新应用程序的数据库主题。如果您需要重新创建数据库,只需将其从数据库服务器中删除或删除文件,它就会自动重新创建。默认情况下,XAF解决方案配置为使用本地系统上的MicrosoftSQLServer Express LocalDB实例,通过集成的安全性进行访问。将在该服务器上以您创建的解决方案的名称创建数据库。但是,XPO和EF Core作为对象关系映射层,支持十几个数据库管理系统。因此,您可以更改默认连接。为此,您需要指定所需的连接设置,并引用所需的数据库提供程序连接器程序集。本主题介绍了允许您将XAF应用程序连接到所需数据库提供程序的几种方法。

Approach 1. Specify the Connection String via the Application Projects’ Configuration Files(方法1.通过应用程序项目的配置文件指定连接字符串)

When you need to access the connection string in a compiled application, specify it via the application projects’ configuration files.
当您需要访问已编译应用程序中的连接字符串时,请通过应用程序项目的配置文件指定它。

  • App.config - a WinForms application project’s configuration file.
    WinForms应用程序项目的配置文件。
  • Web.config - an ASP.NET Web Forms application project’s configuration file.
    ASP.NET Web Forms应用程序项目的配置文件。
  • appsettings.json - an ASP.NET Core Blazor application project’s configuration file.
    ASP.NETCore Blazor应用程序项目的配置文件。

By default, these configuration files contain the connectionStrings section, which has a number of commented sample connection strings. You can uncomment and customize one of the existing connection strings, or specify a new connection string in the following way. Note that by default, XAF application projects are configured to connect to Microsoft SQL Server Express LocalDB.
默认情况下,这些配置文件包含连接字符串部分,其中包含许多注释的示例连接字符串。您可以取消注释并自定义现有连接字符串之一,或者通过以下方式指定新的连接字符串。请注意,默认情况下,XAF应用程序项目配置为连接到MicrosoftSQLServer Express LocalDB。

WinForms and Web Forms

XML 
<connectionStrings>
   <add name="ConnectionString" connectionString=
        "Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\mssqllocaldb;Initial Catalog=MySolution" />
</connectionStrings>

Blazor

JSON 
{
  "ConnectionStrings": {
    "ConnectionString": "Integrated Security=SSPI;Pooling=false;Data Source=(localdb)\\mssqllocaldb;Initial Catalog=MySolution",
    // ...
  },
  // ...
}

Important

  • In XPO applications, the initial connection string declaration sets Pooling to false, thus overriding the default true value and disabling SQL Server Connection Pooling. We do not recommend deleting “Pooling = false;“ or setting Pooling to true. XPO uses a separate connection with disabled pooling to create a new database. If pooling is enabled, a SQL server attempts to create a database and tries to use an existing connection from the pool. This causes an error, because the connection is not notified that the database was created.
    在XPO应用程序中,初始连接字符串声明将池化设置为false,从而覆盖默认真值并禁用SQL服务器连接池。我们不建议删除“Pooling=false;”或将池化设置为true。XPO使用禁用池化的单独连接来创建新数据库。如果启用了池化,SQL服务器会尝试创建数据库并尝试使用池中的现有连接。这会导致错误,因为连接没有被通知数据库已创建。
  • XAF requires Multiple Active Result Sets in EF Core-based applications connected to a Microsoft SQL Server database. We do not recommend that you remove MultipleActiveResultSets=True; from the connection string or set the MultipleActiveResultSets parameter to false. For other database management systems, we implement a mechanism internally that allows an application to correctly run without Multiple Active Result Sets.
    XAF需要连接到MicrosoftSQLServer数据库的基于EF Core的应用程序中的多个活动结果集。我们不建议您从连接字符串中删除MultipleActiveResultSets=True;或将MultipleActiveResultSets参数设置为false。对于其他数据库管理系统,我们在内部实现了一种机制,允许应用程序在没有多个活动结果集的情况下正确运行。

If you connect to a different server, you need to perform two additional steps.
如果您连接到不同的服务器,则需要执行两个额外的步骤。

1.Add the required database provider to the application projects. The complete list of the assemblies required for different database providers can be found in the following documents:
将所需的数据库提供程序添加到应用程序项目中。可以在以下文档中找到不同数据库提供程序所需的程序集的完整列表:
* Database Systems Supported by XPO
* Database Providers (EF Core)

2.In XPO-based applications, specify an appropriate XpoProvider parameter value in the connection string. The complete list of possible values for different database providers can be found in the following section: How to create a correct connection string for XPO providers.
在基于XPO的应用程序中,在连接字符串中指定适当的XpoProvider参数值。不同数据库提供程序的可能值的完整列表可以在以下部分中找到:如何为XPO提供程序创建正确的连接字符串。

In EF Core-based applications, modify an XAF solution as described in the following topic: Switch EF Core Connection from SQL Server to a Different Database Provider.
在基于EF Core的应用程序中,按照以下主题中的说明修改XAF解决方案:将EF Core Connection从SQL服务器切换到不同的数据库提供程序。

In .NET Framework applications, when the connection string is commented or not specified in a configuration file, the connection string specified in the Application Designer is used. This is provided by the following code, which is automatically added in each XAF solution.
在. NET Framework应用程序中,当在配置文件中注释或未指定连接字符串时,将使用应用程序设计器中指定的连接字符串。这由以下代码提供,该代码会自动添加到每个XAF解决方案中。

WinForms

C#
public static void Main(string[] arguments) {
   MySolutionWinApplication winApplication = new MySolutionWinApplication();
   //...
   if(ConfigurationManager.ConnectionStrings["ConnectionString"] != null) {
      winApplication.ConnectionString = ConfigurationManager.
         ConnectionStrings["ConnectionString"].ConnectionString;
   }
   winApplication.Setup();
   winApplication.Start();
   //...
}

ASP.NET Web Forms

C#
public class Global : System.Web.HttpApplication {
   protected void Session_Start(object sender, EventArgs e) {
      WebApplication.SetInstance(Session, new MySolutionWebApplication());
      //...
      if(ConfigurationManager.ConnectionStrings["ConnectionString"] != null) {
         WebApplication.Instance.ConnectionString = ConfigurationManager.
            ConnectionStrings["ConnectionString"].ConnectionString;
      }
      WebApplication.Instance.Setup();
      WebApplication.Instance.Start();
   }
   //...
}

Note
As you can see from the code above, the element named “ConnectionString” is used to obtain the connection string from the configuration file. So do not rename this element unless you change the corresponding string in the application code.
从上面的代码可以看出,名为“ConnectionString”的元素用于从配置文件中获取连接字符串。因此,除非您更改应用程序代码中的相应字符串,否则不要重命名此元素。

Approach 2. Specify the Connection String via the Application Designer (.NET Framework Applications Only)(方法2.通过应用程序设计器指定连接字符串(仅限. NET Framework应用程序))

You can specify the connection string via the Application Designer’s Connection section. Select the Connection item in this section to modify the ConnectionString property in the Properties window. Additionally, you can replace the SQL Connection with another available connection. For this purpose, drag the required connection from the Toolbox to the Connection section, and specify the ConnectionString property as required.
您可以通过应用程序设计器的连接部分指定连接字符串。选择此部分中的连接项以修改属性窗口中的连接字符串属性。此外,您可以将SQL连接替换为另一个可用连接。为此,请将所需的连接从工具箱拖到连接部分,并根据需要指定连接字符串属性。
在这里插入图片描述

When you drag the required connection from the Toolbox to the Connection section, the required database provider assembly is automatically referenced. If the required connection component is not available, right-click the Toolbox to invoke the Choose Toolbox Items dialog, tick off the required connection component and click Ok.
当您将所需的连接从工具箱拖到连接部分时,将自动引用所需的数据库提供程序程序集。如果所需的连接组件不可用,请右键单击工具箱以调用选择工具箱项对话框,勾选所需的连接组件并单击确定。

Note
The connection string specified in the application configuration file overrides the connection string specified in the Application Designer. This is done to provide application administrators with an option to change the database connection without recompiling the application.
应用程序配置文件中指定的连接字符串会覆盖应用程序设计器中指定的连接字符串。这样做是为了向应用程序管理员提供更改数据库连接的选项,而无需重新编译应用程序。

Get the Connection String to a Database (XPO-Based Approach)(获取到数据库的连接字符串(基于XPO的方法))

You can obtain properly formatted connection strings via XPO data store adapter classes. These classes are located in the DevExpress.Xpo.DB namespace of the DevExpress.Xpo.v24.1 assembly. Each adapter class contains the GetConnectionString method. Use this method to obtain a connection string to the database using specified parameters. All XPO adapter classes are listed in the Database Systems Supported by XPO topic.
您可以通过XPO数据存储适配器类获取格式正确的连接字符串。这些类位于DevExpress. Xpo.v24.1程序集的DevExpress.Xpo.DB命名空间中。每个适配器类都包含GetConnectionString方法。使用此方法可以使用指定的参数获取到数据库的连接字符串。所有XPO适配器类都列在XPO支持的数据库系统主题中。

The following code snippet demonstrates how to connect to a “MyApp” MySql database located on the “DBSERVER” server using the “usr” account with the “pwd” password. The MySql.Data assembly must be referenced in the application project.
以下代码片段演示了如何使用带有“pwd”密码的“usr”帐户连接到位于“DBSERVER”服务器上的“MyApp”MySql数据库。必须在应用程序项目中引用MySql.Data程序集。

WinForms

C#
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
//...
public static void Main(string[] arguments) {
   MySolutionWinApplication winApplication = new MySolutionWinApplication();
   //...
   winApplication.ConnectionString = 
      MySqlConnectionProvider.GetConnectionString("DBSERVER", "usr", "pwd", "MyApp");
   winApplication.Setup();
   winApplication.Start();
   //...
}

ASP.NET Web Forms

C#
using DevExpress.Xpo;
using DevExpress.Xpo.DB;
//...
public class Global : System.Web.HttpApplication {
   protected void Session_Start(object sender, EventArgs e) {
      WebApplication.SetInstance(Session, new MySolutionWebApplication());
      //...
      WebApplication.Instance.ConnectionString = 
         MySqlConnectionProvider.GetConnectionString("DBSERVER", "user", "pwd", "MyApp");
      WebApplication.Instance.Setup();
      WebApplication.Instance.Start();
   }
   //...
}

If you want to specify the connection string using the first or the second approach described above, look at the value returned by the GetConnectionString method in the debugger, and copy it to the Application Designer or the configuration file. Note that a string returned by the GetConnectionString method contains the additional XpoProvider parameter. It identifies the database type to which a connection should be established. So, a string returned by the GetConnectionString method is not fully compatible with the standard connection string format. You can use this connection string in configuration files without modification, but the extra parameter has to be removed to use the string in the Application Designer.
如果要使用上述第一种或第二种方法指定连接字符串,请在调试器中查看GetConnectionString方法返回的值,并将其复制到Application Designer或配置文件中。请注意,GetConnectionString方法返回的字符串包含附加的XpoProvider参数。它标识应该建立连接的数据库类型。因此,GetConnectionString方法返回的字符串与标准连接字符串格式不完全兼容。您可以在配置文件中使用此连接字符串而无需修改,但必须删除额外的参数才能在Application Designer中使用该字符串。

标签:15,string,database,应用程序,Application,connection,字符串,连接,XAF
From: https://blog.csdn.net/thomastang200912_126/article/details/143483756

相关文章

  • 【数据集】【YOLO】【目标检测】道路结冰数据集 1527 张,YOLO目标检测实战训练教程!
    数据集介绍【数据集】道路结冰数据集1527张,目标检测,包含YOLO/VOC格式标注。数据集中包含2种分类:“clear_road,ice_road”。数据集来自国内外图片网站和视频截图,部分数据经过数据增强处理。检测范围监控视角检测、无人机视角检测、道路结冰、道路湿滑等,可用于智慧园区、智慧......
  • 代码随想录算法训练营第十一天|leetcode150. 逆波兰表达式求值、leetcode239. 滑动窗
    1leetcode150.逆波兰表达式求值题目链接:150.逆波兰表达式求值-力扣(LeetCode)文章链接:代码随想录视频链接:栈的最后表演!|LeetCode:150.逆波兰表达式求值哔哩哔哩bilibili自己的思路:这是一道有思路,但是思路并不多的题目,就是我会觉得是先将数据进行添加,然后对于符号通过......
  • 20222315 2024-2025-1 《网络与攻防技术》实验四实验报告
    1、实验内容1.1、恶意代码文件类型标识、脱壳与字符串提取对提供的rada恶意代码样本,进行文件类型识别,脱壳与字符串提取,以获得rada恶意代码的编写作者,具体操作如下:(1)使用文件格式和类型识别工具,给出rada恶意代码样本的文件格式、运行平台和加壳工具;(2)使用超级巡警脱壳机等脱壳软......
  • 2024-2025-1 20241415 《计算机基础与程序设计》第六周学习总结
    2024-2025-120241415《计算机基础与程序设计》第六周学习总结作业信息这个作业属于哪个课程2024-2025-1-计算机基础与程序设计这个作业要求在哪里2024-2025-1计算机基础与程序设计第六周作业这个作业的目标Polya如何解决问题、简单类型与组合类型、复合数据结构......
  • luoguP2015 二叉苹果树
    给定一棵N个节点的苹果树,根节点编号为1。如果树枝有分叉,一定是分二叉。已知节点a与b的边权为w[a][b]。求一棵树,最多有Q条边,并且边权之和最大。1<=Q<N<=100;0<=w[i][j]<=3E4分析:Q条边的树对应Q+1个节点,转化为节点数限制,可以用树上背包的方法来做。记dp[x][j]表示以x为根,选择节点......
  • The 3rd Universal Cup. Stage 15: Chengdu
    A.ArrowaRow一个简单的构造题,构造的思路是先把又侧的连续>放满,再从左侧逐个开始放,如果是>就放一个长度为5的,如果是-,可以一次性直接把连续的都放了。#include<bits/stdc++.h>usingnamespacestd;usingi32=int32_t;usingi64=longlong;usingvi=vector<int>......
  • 2024-2025-1 学号20241315《计算机基础与程序设计》第六周学习总结
    作业信息这个作业属于哪个课程2024-2025-1-计算机基础与程序设计这个作业要求在哪里https://www.cnblogs.com/rocedu/p/9577842.html#WEEK06这个作业的目标Polya如何解决问题简单类型与组合类型复合数据结构查找与排序算法算法复杂度递归代码安全作业正文......
  • 【LeetCode:153. 寻找旋转排序数组中的最小值 + 二分】
    在这里插入代码片......
  • Veritas Enterprise Vault 15.1 (Windows) - 自动捕获数据并归档信息
    VeritasEnterpriseVault15.1(Windows)-自动捕获数据并归档信息信息归档解决方案,确保合规与有效的信息治理请访问原文链接:https://sysin.org/blog/veritas-enterprise-vault-15/查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgEnterpriseVault独家技术打造的......
  • 代码随想录算法训练营第九天|leetcode151.翻转字符串里的单词、卡码网55.右旋字符串、
    1leetcode151.翻转字符串里的单词题目链接:151.反转字符串中的单词-力扣(LeetCode)文章链接:代码随想录视频链接:字符串复杂操作拿捏了!|LeetCode:151.翻转字符串里的单词哔哩哔哩bilibili自己的思路:直接将空格去掉,然后分割字符串为列表,在列表中进行翻转,不在字符串内部操作......