通用调用存储过程以从 C# 下载数据
Generic Calls to stored procedure for data download from C#
我想根据一组用户选择和用户将下载的 return 数据调用多个存储过程。每个存储过程都有一组不同的参数列表和类型。这些参数将作为表单元素通过网页公开。
我的问题主要是对从 C# 调用存储过程的通用机制有其他想法,而不管参数的类型如何。
我目前正在考虑配置一个 xml 文件,该文件将定义如何调用存储过程并根据参数类型列表动态解析输入并调用给定的存储过程。
如果有使用 .Net 框架中可用的任何其他机制的任何其他想法,我将非常高兴。不知道有没有更好的方法。
非常感谢
b
还有其他方法可以做到这一点,但我不确定它们是否更好。
一种方法是动态使用 SqlCommandBuilder.DeriveParameters
method to populate your SqlCommand.Parameters
。
使用此方法的主要优点是您无需将参数列表保存在存储过程本身之外的其他位置,从而使您的维护更加容易。此外,它还使您的代码更短更清晰。
缺点是它需要为每个存储过程调用一次数据库,这可能比解析一个 XML 文件要慢一些。
请注意我链接到的 DeriveParameters MSDN 页面上的备注部分:
DeriveParameters overwrites any existing parameter information for the SqlDbCommand.
DeriveParameters requires an additional call to the database to obtain the information. If the parameter information is known in advance, it is more efficient to populate the parameters collection by setting the information explicitly.
You can only use DeriveParameters with stored procedures. You cannot use DeriveParameters with extended stored procedures. You cannot use DeriveParameters to populate the SqlParameterCollection with arbitrary Transact-SQL statements, such as a parameterized SELECT statement.
使用方法很简单:
SqlCommand cmd;
Using(SqlConnection Con = new SqlConnection("ConnectionString")
{
cmd = new SqlCommand("StoredProcedureName", Con);
cmd.CommandType = CommandType.StoredProcedure;
Con.Open();
SqlCommandBuilder.DeriveParameters(cmd);
Con.Close();
}
执行此代码后,cmd.Parameters 将包含所有可以使用的参数,您只需为它们设置值即可。
我建议创建一个方法来封装此代码并获取 SqlCommend 作为参数。此外,您可能希望创建一个方法,该方法将 return 最适合每个参数的输入控件(即 DatePicker
用于 SqlDBType.Date
,TextBox
用于 SqlDBType.Varchar
等等`).
我想根据一组用户选择和用户将下载的 return 数据调用多个存储过程。每个存储过程都有一组不同的参数列表和类型。这些参数将作为表单元素通过网页公开。 我的问题主要是对从 C# 调用存储过程的通用机制有其他想法,而不管参数的类型如何。
我目前正在考虑配置一个 xml 文件,该文件将定义如何调用存储过程并根据参数类型列表动态解析输入并调用给定的存储过程。
如果有使用 .Net 框架中可用的任何其他机制的任何其他想法,我将非常高兴。不知道有没有更好的方法。
非常感谢 b
还有其他方法可以做到这一点,但我不确定它们是否更好。
一种方法是动态使用 SqlCommandBuilder.DeriveParameters
method to populate your SqlCommand.Parameters
。
使用此方法的主要优点是您无需将参数列表保存在存储过程本身之外的其他位置,从而使您的维护更加容易。此外,它还使您的代码更短更清晰。
缺点是它需要为每个存储过程调用一次数据库,这可能比解析一个 XML 文件要慢一些。
请注意我链接到的 DeriveParameters MSDN 页面上的备注部分:
DeriveParameters overwrites any existing parameter information for the SqlDbCommand.
DeriveParameters requires an additional call to the database to obtain the information. If the parameter information is known in advance, it is more efficient to populate the parameters collection by setting the information explicitly.
You can only use DeriveParameters with stored procedures. You cannot use DeriveParameters with extended stored procedures. You cannot use DeriveParameters to populate the SqlParameterCollection with arbitrary Transact-SQL statements, such as a parameterized SELECT statement.
使用方法很简单:
SqlCommand cmd;
Using(SqlConnection Con = new SqlConnection("ConnectionString")
{
cmd = new SqlCommand("StoredProcedureName", Con);
cmd.CommandType = CommandType.StoredProcedure;
Con.Open();
SqlCommandBuilder.DeriveParameters(cmd);
Con.Close();
}
执行此代码后,cmd.Parameters 将包含所有可以使用的参数,您只需为它们设置值即可。
我建议创建一个方法来封装此代码并获取 SqlCommend 作为参数。此外,您可能希望创建一个方法,该方法将 return 最适合每个参数的输入控件(即 DatePicker
用于 SqlDBType.Date
,TextBox
用于 SqlDBType.Varchar
等等`).