获取ADO.NET中的参数前缀

发布于 2024-11-27 12:28:14 字数 963 浏览 0 评论 0原文

我想使用列名作为参数基于列列表生成多个 SQL 语句。

编辑:C#

var columns = new string[] { "COL1", "COL2" };
var tableName = "TABLE_1";
var prefix = "@"; // TODO get this from the provider factory

string sqlInsert = string.Format(
    "INSERT INTO {0}\n( {1}) VALUES\n({2})",
    tableName,
    string.Join(",  ", columns),
    string.Join(", ", columns.Select(c => prefix + c)));

生成的 SQL:

INSERT INTO TABLE_1
( COL1,  COL2) VALUES
(@COL1, @COL2)

这适用于 SqlClient。但我使用 System.Data 中的抽象类(DbCommandDbParameter 等)以及基于连接的不同数据提供程序,例如 Oracle、MySQL、Postgres 等app.config 中的字符串设置。因此我需要知道我必须使用哪个前缀。对于MS-SQL,它是@,Oracle使用:,其余的我实际上不知道。

有没有办法从提供者工厂获取此前缀字符?

System.Data.SqlClient.SqlClientFactory.Instance没有这样的信息,或者至少我找不到它。

否则,你能给我一个常用数据库的列表吗?

编辑:目标平台是 .NET 2 到 .NET 4,信息应该可以通过提供程序工厂获得。

I want to generate several SQL statements based on a column list using the column names as parameters.

Edit: C#

var columns = new string[] { "COL1", "COL2" };
var tableName = "TABLE_1";
var prefix = "@"; // TODO get this from the provider factory

string sqlInsert = string.Format(
    "INSERT INTO {0}\n( {1}) VALUES\n({2})",
    tableName,
    string.Join(",  ", columns),
    string.Join(", ", columns.Select(c => prefix + c)));

Generated SQL:

INSERT INTO TABLE_1
( COL1,  COL2) VALUES
(@COL1, @COL2)

This works with the SqlClient. But I'm using the abstract classes in System.Data (DbCommand, DbParameter, etc.) and different data providers such as Oracle, MySQL, Postgres etc based on the connection string settings in the app.config. Thus I need to know which prefix I have to use. For MS-SQL it is the @, Oracle uses the :, the rest I actually don't know.

Is there a way to get this prefix character from the provider factory?

System.Data.SqlClient.SqlClientFactory.Instancedoesn't have such an information or at least I couldn't find it.

Otherwise, can you give me a list for the common databases?

Edit: The target platforms are .NET 2 to .NET 4 and the information should be available through the provider factory.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眉黛浅 2024-12-04 12:28:14

有 2 个 DbCommandBuilder 方法可以帮助您,GetParameterNameGetParameterPlaceholder。这些是受保护的,因此您需要一些思考才能使用它们。

查看我对以下问题的回答以了解实现(也包含在 DbExtensions 库中):

<一个href="https://stackoverflow.com/questions/271398/what-are-your-favorite-extension-methods-for-c-codeplex-com-extensionoverflow/2550726#2550726">您最喜欢的 C# 扩展方法是什么? (codeplex.com/extensionoverflow)

There are 2 DbCommandBuilder methods that will help you, GetParameterName and GetParameterPlaceholder. These are protected, so you'll need a little reflection to use them.

Check out my answer to the following question for an implementation (also included in the DbExtensions library):

What are your favorite extension methods for C#? (codeplex.com/extensionoverflow)

辞取 2024-12-04 12:28:14

我找到了答案,但无法重现我是如何找到它的:

http://www.codewrecks.com/blog/index.php/2007/09/06/about-parametermarkerformat/

DbConnection 可以提供一个架构表,其中也包含正确的用于创建命令参数名称的格式字符串,SqlClient 除外!!

DbProviderFactory myFactory = DbProviderFactories.GetFactory(myProviderName);

using (DbConnection myConnection = myFactory.CreateConnection())
{
    myConnection.ConnectionString = mySettings.ConnectionString;
    myConnection.Open();

    string parameterMarker = myConnection
        .GetSchema(DbMetaDataCollectionNames.DataSourceInformation)
        .Rows[0][DbMetaDataColumnNames.ParameterMarkerFormat].ToString();

    myConnection.Close();
}

对于 SqlCclient parameterMarker{0} 但应该为 @{0}。我将进一步调查以找出其他架构表列中包含的内容。

I found the answer, but I cannot reproduce how I found it:

http://www.codewrecks.com/blog/index.php/2007/09/06/about-parametermarkerformat/

The DbConnection can provide a schema table that also contains the correct format strings for creating command parameter names, except for SqlClient!!

DbProviderFactory myFactory = DbProviderFactories.GetFactory(myProviderName);

using (DbConnection myConnection = myFactory.CreateConnection())
{
    myConnection.ConnectionString = mySettings.ConnectionString;
    myConnection.Open();

    string parameterMarker = myConnection
        .GetSchema(DbMetaDataCollectionNames.DataSourceInformation)
        .Rows[0][DbMetaDataColumnNames.ParameterMarkerFormat].ToString();

    myConnection.Close();
}

For SqlCclient parameterMarker is {0} but should be @{0}. I'll investigate a bit more to find out what is contained in the other schema table columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文