.net:如何在 C# 中执行带有可为空参数的存储过程?

标签 .net sql-server stored-procedures parameters nullable

如何在 C# 中执行带有可空参数的存储过程?

编辑:

实际上,我已经写了下面的代码。如您所见,status 参数是一个可以为 null 的值类型。 这是对的吗?还是不是?

public void LoadAll(DataTable tb, int? status=null)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection())
            {
                connection.ConnectionString = this.connectionString;

                using (SqlCommand command = connection.CreateCommand())
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.CommandText = "USP_OrganizationChartSelect";
                    SqlCommandBuilder.DeriveParameters(command);
                    command.Parameters["@Status"].Value = status.HasValue ? status : null;
                    if (connection.State != ConnectionState.Open)
                        connection.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    tb.Clear();
                    adapter.Fill(tb);
                    adapter.Dispose();
                    adapter = null;
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

谢谢

最佳答案

你可以试试 DBNull.Value而不是 null 或在 nullable 完全没有值时省略参数。

关于.net:如何在 C# 中执行带有可为空参数的存储过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2947540/

相关文章:

SQL Server 按类别列表

sql - MSSQL - 左连接右表中包含空值

c# - 如何准确测量Azure Web应用程序中的 "data out"?

SQL Server - 加快大表的计数

java - 访问从 oracle 过程返回的数组

MySQL 存储过程作为普通 SQL 工作

mysql - 在 mysql workbench 5.5 上定义事件名称时出现错误

c# - 将查询字符串转换为 .Net 中的键值对

c# - MVVM 中的什么使它对托管 WPF 和 Silverlight 特别有吸引力,但对 native C++ 却没有吸引力?

c# - C# 编译器的优化程度如何?