这还能更好吗? SQL Server2005的.NET 2.0兼容性:
public static SqlString RegexSubstring(SqlString regexpattern,
SqlString sourcetext,
SqlInt32 start_position)
{
SqlString result = null;
if (!regexpattern.IsNull && !sourcetext.IsNull && !start_position.IsNull)
{
int start_location = (int)start_position >= 0 ? (int)start_position : 0;
Regex RegexInstance = new Regex(regexpattern.ToString());
result = new SqlString(RegexInstance.Match(sourcetext.ToString(),
start_location).Value);
}
return result;
}这是我第一次尝试为SQL Server编写CLR函数/etc-参数一定要使用SqlString/etc数据类型吗?
发布于 2010-05-13 05:19:19
只需通过Refactor/Pro运行它
给出了这个:
public static SqlString RegexSubstring(SqlString regexpattern,
SqlString sourcetext,
SqlInt32 start_position) {
if (regexpattern.IsNull || sourcetext.IsNull || start_position.IsNull)
return null;
Regex RegexInstance = new Regex(regexpattern.ToString());
return new SqlString(RegexInstance.Match(sourcetext.ToString(),
(int)start_position).Value);
}请注意,start_location未使用,因此您可能忽略了警告?
另一件事只是一个风格问题,但是函数可以编写成不依赖于SqtTypes吗?然后代码变成:
private static string RegexSubstring(string regexpattern, string sourcetext, int start_position) {
if (regexpattern == null || sourcetext == null || start_position == null)
return null;
Regex RegexInstance = new Regex(regexpattern);
return RegexInstance.Match(sourcetext, start_position).Value;
}并使用以下命令调用它:
new SqlString(RegexSubstring(regexpattern.ToString(), sourcetext.ToString(), start_position))https://stackoverflow.com/questions/2822800
复制相似问题