c# - 添加可选参数

标签 c#

我想在下面的代码中为objTest.TestNumber提供可选的参数值。我的代码如下:

 public class TestInfo
{
    public int Id { get; set; }
    public string TestNumber { get; set; }
}       



public string TestUpdate(TestInfo objTest)
    {
        int retVal = 0;
        try
        {
            dataContext = new AccretiveAPIContext(GetConnection(locationId));
            retVal = dataContext.Database.ExecuteSqlCommand(
                     "EXEC usp_TestUpdate @ID, @TestNumber",
                     new SqlParameter("@ID", objTest.Id),
                     new SqlParameter("@TestNumber", objTest.TestNumber),

                 );
        }
        catch (Exception)
        {
            throw;
        }
        return retVal.ToString();
    }


请告诉我为使“ @TestNumber”,objTest.TestNumber作为可选参数而需要进行的更改。我想将其默认值设置为零(“ 0”)。

请帮忙。

最佳答案

甚至更容易-

            retVal = dataContext.Database.ExecuteSqlCommand(
                 "EXEC usp_TestUpdate @ID, @TestNumber",
                 new SqlParameter("@ID", objTest.Id),
                 new SqlParameter("@TestNumber", string.IsNullOrEmpty(objTest.TestNumber) ? "0" : objTest.TestNumber),

关于c# - 添加可选参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35700385/

相关文章:

c# - IdentityServer4 AddSigningCredentials 与证书

c# - 中继器链接按钮 Onclick 未触发

c# - 用户代码未处理 PriorException

c# - 如何访问斯坦福解析器返回的依赖树和选区树中的各个节点?

c# - 当作为泛型参数传递时,为什么泛型类型不被识别为其派生类型?

c# - Resharper 一直提示命名空间与文件位置不对应,即使它确实如此

c# - 自动映射类型为接口(interface)的属性

c# - 为什么 Visual Studio 的编辑器中显示 (-) 标记?

c# - WTSQueryUserToken 在 C# 的 Windows 7 上总是抛出 "An attempt was made to reference a token that does not exist"

c# - 使用 Process.Start(),当/K 参数不起作用时如何保持 cmd 提示符打开?