获取ADO.NET中的参数前缀
我想使用列名作为参数基于列列表生成多个 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 中的抽象类(DbCommand
、DbParameter
等)以及基于连接的不同数据提供程序,例如 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.Instance
doesn'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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有 2 个 DbCommandBuilder 方法可以帮助您,GetParameterName 和 GetParameterPlaceholder。这些是受保护的,因此您需要一些思考才能使用它们。
查看我对以下问题的回答以了解实现(也包含在 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)
我找到了答案,但无法重现我是如何找到它的:
http://www.codewrecks.com/blog/index.php/2007/09/06/about-parametermarkerformat/
DbConnection 可以提供一个架构表,其中也包含正确的用于创建命令参数名称的格式字符串,SqlClient 除外!!
对于
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!!
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.