Ms Access 和 SqlServer 的参数发现。 使用 Microsoft Patterns and Practices DataBlock 版本 3.0 final






2.33/5 (3投票s)
2007 年 3 月 7 日
4分钟阅读

30362

279
使用数据库架构辅助参数生成命令。
Ms Access 和 SqlServer 的参数发现。 使用 Microsoft Patterns and Practices DataBlock April Ver 3.0 Final
这是我上一篇文章的更新
为 Defenition Tool 添加了另一个选项卡页面,这将正确更改程序编译所需的属性。
此项目包含 Ms Access Jet 4.00 数据库或 SqlServer,收集有助于为存储过程和 SQL 查询命令生成命令参数的架构信息。“Generics.cs”是 Data.dll 用于 Ms Access 的类,如果您尝试发现参数,它会抛出异常,因为它无法做到。我将我的架构收集类添加到了这个方法中,因此主抽象类可以继续。将命令参数添加到内部缓存(下次使用此命令时,它将使用缓存版本)并执行“NonQuery”命令到数据库。
背景
我在更新过程中遇到了不少问题
使用 Enterprise Library Configuration 工具时,该工具会引用强命名版本的 Dll。
这使得我的 DataModified.dll 无效,在使用 Enterprise Library Configuration 工具后,您必须更改 app.config 上的属性以指向我的
Dll
临时解决方案
为 Defenition Tool 添加了另一个选项卡页面,这将正确更改程序编译所需的属性。
再次
为了使用 Enterprise Library Configuration 工具,引用应如下所示
"dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
为了使我的 Dll 能够编译,属性需要如下所示。
"dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.DataModified, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null" />
构建定义(defenitions)的工具包含多个选项卡页面,最后一个 tabPage 将在上述两个值之间更改属性。
请参阅文档以获取更多信息。
使用代码
选择 XML 文档,可以是 App.config 或您选择的任何 Xml 文档。
可选,一个小的程序,它修改现有的 Xml 文档并添加数据库架构,否则。只需使用此版本的 Data.dll,而不是标准版本。提供了一个数据层,该数据层注释很多,看起来可能有点吓人,直到您删除注释。
请注意
如果您不想使用 XML 选项。
我添加了一个自定义节到 App.config,指向要查找的 Xml 文档,该节需要有一个值或 null。
示例
这是我重载的一个示例,SQL 查询版本。
// public int ExecuteQueryCommand(string SqlQuery, Object[] ObjParameters) // { // Database database = DatabaseFactory.CreateDatabase("AccessPhotoAlbum"); // DbCommand Command = database.GetStoredProcCommand(CommandType.Text, // SqlQuery, ObjParameters); // int nRowsAffected = database.ExecuteNonQuery(Command); // return nRowsAffected; // }
我大部分时间都使用这个。在这两个示例中,默认数据库都被名为“AccessPhotoAlbum”的第二个数据库覆盖。
// public int ExecuteQueryCommand(string SqlQuery, Object[] ObjParameters)
// {
// Database database = DatabaseFactory.CreateDatabase("AccessPhotoAlbum");
// DbCommand Command = database.GetStoredProcCommand(
// CommandType.StoredProcedure, SqlQuery, ObjParameters);
// int nRowsAffected = database.ExecuteNonQuery(Command);
// return nRowsAffected;
// }
这是自动编号检索的一个示例。
请注意:此命令使用一个输出参数,以返回“受影响的行数”和“新 ID”。
// using Stored Procedure, two new overloads used here,
// Command Type was declared at
// form level and passed to this method only
// to be used on InsertCommands on Ms Access.
//public int InsertGetIdentity(string strSpName, CommandType Ctype,
// Object[] ObjParameters, out int nReturnValue)
// {
// Database database = DatabaseFactory.CreateDatabase("AccessPhotoAlbum");
// DbCommand Command = database.GetStoredProcCommand(Ctype,
// strSpName, ObjParameters);
//
// // new overloaded method.
// int nRowsAffected = database.ExecuteNonQuery(Command, out nReturnValue);
// Debug.Print("");
// Debug.Print("--------------------------");
// string DebugMsg = String.Format("Returned Identity: {0} ",
// nReturnValue.ToString());
// Debug.Print(DebugMsg);
// return nRowsAffected;
// }
新增类
这些可以在 Data.dll 内名为“CustomMethods”的新目录中找到。
借用的类 - Profile Dll
I borrowed this Dll from this site. Title : AMS.profile page title : Read/Write XML files, Config files, INI files, or the Registry Author : Alvaro Mendez. page : http://codeproject.org.cn/csharp/readwritexmlini.asp it can write to App.config, XMl,Registry or InI all under using same profile. Quite cool, but only add/edits key values (in any section).
历史
08/04/07:
更新到最终版本 3.0
01/03/07:
添加了可选的 XML 文档来接受数据库定义(DataBase Defenition),我添加这个是因为我最喜欢这种方法。
它不需要连接数据库,也不需要使用哈希表。
27/12/06:
在我和其他程序员(业余爱好者)进行了一两次对话后,我决定添加一个选项。如果您将一个 Xml 文件放在可执行目录中,该类将读取它,并使用它来构建命令。
它以该类能够处理的最佳方式呈现字段名称和值。我制作了一个小型程序,它将修改现有的 XML 文档并添加数据库的架构。问题是,如果您更改了数据库结构,它就需要更新,所以要小心,您可能会发现存储过程无缘无故地无法构建,现在您知道了原因。
如果您打算使用它,请保持更新,否则将其从文件夹中删除,并将 Appconfig 中的值“DabaseDefenitionFile”设置为 null,类将继续正常运行。
04/12/06:
为了保持针对 SQL Server 的高度代码定向,我被迫向 data.dll 添加了两个重载,这些重载是为 MS Access 使用而添加的,但也可以由 SQL Server 使用,前提是您选择使用它们。所有重载都有清晰简洁的注释,指示“针对 Ms Access 定制的自定义方法”。向 Data.Dll 添加了重载,并在“Generics.cs”中进行了覆盖,以帮助检索“SELECT @@IDENTITY”。
29/11/06:
添加了对 DataSet 参数发现的支持。我在这个方面有得有失,总的来说非常满意。我仍在研究关于这个问题的更多细节,可能会有其他更新。
23/11/06:
添加了对 SQL Server 查询参数发现的支持,我添加这个主要是为了早期设计帮助,并且它使我的数据层在两种数据库类型之间保持一致。