验证SQL参数值是否为正确的类型c#
您好,我正在寻找一种方法来验证添加到验证SQL参数值是否为正确的类型c#,c#,sql,.net,C#,Sql,.net,您好,我正在寻找一种方法来验证添加到SqlCommandas参数的字符串是否正确。 这是我的密码: string wrongType = "This is not a date"; command.Parameters.Add("@Date", SqlDbType.DateTime).Value = wrongType; 是否有方法检查错误类型是否可以转换为SqlDbType.DateTime?您可以使用DateTime.trypare: string wrongType = "This is
SqlCommand
as参数的字符串是否正确。
这是我的密码:
string wrongType = "This is not a date";
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = wrongType;
是否有方法检查
错误类型
是否可以转换为SqlDbType.DateTime
?您可以使用DateTime.trypare
:
string wrongType = "This is not a date";
DateTime rightTyped;
if(DateTime.TryParse(wrongType, out rightTyped))
{
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = rightTyped;
}
您可以使用
DateTime.TryParse
:
string wrongType = "This is not a date";
DateTime rightTyped;
if(DateTime.TryParse(wrongType, out rightTyped))
{
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = rightTyped;
}
您可以使用
DateTime.TryParse
:
string wrongType = "This is not a date";
DateTime rightTyped;
if(DateTime.TryParse(wrongType, out rightTyped))
{
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = rightTyped;
}
您可以使用
DateTime.TryParse
:
string wrongType = "This is not a date";
DateTime rightTyped;
if(DateTime.TryParse(wrongType, out rightTyped))
{
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = rightTyped;
}
如果获得用户输入,则必须在适当的位置验证输入 试试这个:
string wrongType = "This is not a date";
DateTime date;
if(DateTime.TryParse(wrongType, out date))
{
// staff when string converted
}
如果获得用户输入,则必须在适当的位置验证输入 试试这个:
string wrongType = "This is not a date";
DateTime date;
if(DateTime.TryParse(wrongType, out date))
{
// staff when string converted
}
如果获得用户输入,则必须在适当的位置验证输入 试试这个:
string wrongType = "This is not a date";
DateTime date;
if(DateTime.TryParse(wrongType, out date))
{
// staff when string converted
}
如果获得用户输入,则必须在适当的位置验证输入 试试这个:
string wrongType = "This is not a date";
DateTime date;
if(DateTime.TryParse(wrongType, out date))
{
// staff when string converted
}
问题是第一行:
string wrongType = "This is not a date";
如果它必须是一个日期,并且如果您的TSQL是针对一个日期工作的,那么使用日期:
DateTime rightType = ...
现在它从来没有错过。基本上,停止依赖字符串。代码的其余部分仍然类似:
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = rightType;
请注意,您可以使用
DateTime.Parse
和DateTime.TryParse
从字符串输入获取日期时间。问题在于第一行:
string wrongType = "This is not a date";
如果它必须是一个日期,并且如果您的TSQL是针对一个日期工作的,那么使用日期:
DateTime rightType = ...
现在它从来没有错过。基本上,停止依赖字符串。代码的其余部分仍然类似:
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = rightType;
请注意,您可以使用
DateTime.Parse
和DateTime.TryParse
从字符串输入获取日期时间。问题在于第一行:
string wrongType = "This is not a date";
如果它必须是一个日期,并且如果您的TSQL是针对一个日期工作的,那么使用日期:
DateTime rightType = ...
现在它从来没有错过。基本上,停止依赖字符串。代码的其余部分仍然类似:
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = rightType;
请注意,您可以使用
DateTime.Parse
和DateTime.TryParse
从字符串输入获取日期时间。问题在于第一行:
string wrongType = "This is not a date";
如果它必须是一个日期,并且如果您的TSQL是针对一个日期工作的,那么使用日期:
DateTime rightType = ...
现在它从来没有错过。基本上,停止依赖字符串。代码的其余部分仍然类似:
command.Parameters.Add("@Date", SqlDbType.DateTime).Value = rightType;
请注意,您可以使用
DateTime.Parse
和DateTime.TryParse
从字符串输入获取日期时间。Ok,如果输入来自用户呢?(我猜这是OP的情况)。@Matias然后使用parse/tryparse。您当然不会将其作为字符串发送到数据库。这也是为什么您应该将DB代码和UI代码分开的原因。当然,您不会发送字符串。好的,若输入来自用户呢?(我猜这是OP的情况)。@Matias然后使用parse/tryparse。您当然不会将其作为字符串发送到数据库。这也是为什么您应该将DB代码和UI代码分开的原因。当然,您不会发送字符串。好的,若输入来自用户呢?(我猜这是OP的情况)。@Matias然后使用parse/tryparse。您当然不会将其作为字符串发送到数据库。这也是为什么您应该将DB代码和UI代码分开的原因。当然,您不会发送字符串。好的,若输入来自用户呢?(我猜这是OP的情况)。@Matias然后使用parse/tryparse。您当然不会将其作为字符串发送到数据库。这也是为什么您应该将DB代码和UI代码分开。当然,您不会发送字符串。这段代码很奇怪,因为它在失败的情况下无法添加参数(但仍尝试正常工作)。它还隐式地提倡让UI代码(解析等)与DB代码过于接近。当你的代码谈论DB关注点时,它不应该有任何原始输入——它们应该已经被解析了,依我看。@MarcGravel我理解你的关注点,但你知道这个问题非常一般,因此,答案将是一般的。我的回答并不是在描述你的担忧:这和问题本身一样过于简单化了,不是吗?:)@MarcGravell我打算编辑添加一个else
,以便抛出一个可能的异常,如FormatException
,但它没有使用TryParse
…这段代码很奇怪,因为它在失败的情况下没有添加参数(但仍然尝试正常工作)。它还隐式地提倡让UI代码(解析等)与DB代码过于接近。当你的代码谈论DB关注点时,它不应该有任何原始输入——它们应该已经被解析了,依我看。@MarcGravel我理解你的关注点,但你知道这个问题非常一般,因此,答案将是一般的。我的回答并不是在描述你的担忧:这和问题本身一样过于简单化了,不是吗?:)@MarcGravell我打算编辑添加一个else
,以便抛出一个可能的异常,如FormatException
,但它没有使用TryParse
…这段代码很奇怪,因为它在失败的情况下没有添加参数(但仍然尝试正常工作)。它还隐式地提倡让UI代码(解析等)与DB代码过于接近。当你的代码谈论DB关注点时,它不应该有任何原始输入——它们应该已经被解析了,依我看。@MarcGravel我理解你的关注点,但你知道这个问题非常一般,因此,答案将是一般的。我的回答并不是在描述你的担忧:这和问题本身一样过于简单化了,不是吗?:)@MarcGravell我打算编辑添加一个else
,以便抛出一个可能的异常,如FormatException
,但它没有使用TryParse
…这段代码很奇怪,因为它在失败的情况下没有添加参数(但仍然尝试正常工作)。它还隐式地提倡让UI代码(解析等)与DB代码过于接近。当你的代码谈论数据库问题时,它不应该有任何原始输入——依我看,它们应该已经被解析了