我创建了一些用户定义的类型作为表值参数.有没有什么方法可以选择他们的列,就像我可以为表选择列:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE 'MyTable'
Run Code Online (Sandbox Code Playgroud)
编辑:是的,我试图阅读MSDN,但我没有看到那里的信息.我目前的解决方法是创建一个永久表,如下所示:
CREATE TABLE Placeholder(copy-and-paste all the columns from my type here)
Run Code Online (Sandbox Code Playgroud)
然后我可以从INFORMATION_SCHEMA.COLUMNS中选择并在完成后删除占位符.
我想重构一些C#模块,它们通过Linq-to-SQL从SQL Server 2008读取数据.这些存储过程最多获取一行,因为我提交完整的PK作为参数.显然Linq-to-SQL并不知道最多可以返回一行.因此,以下代码运行以获取一个值或抛出异常:
var results = context.MyProcName(myParameter);
foreach (var result in results)
{
return result.THeColumnINeed;
}
throw new Exception(string.Format("Value not found for parameter {0}", myParameter));
Run Code Online (Sandbox Code Playgroud)
这段代码完成了工作,但有点看起来很难看.我怎样才能做得更好?
我有很多集成测试可以读取文件的预期结果.我的测试通过相对路径访问这些文件.相对路径是x86与任何CPU的不同深度级别.例如,当我的测试在x86下运行时,他们需要读取以下文件"../../TestResults/MyTest.csv",但在任何CPU下他们需要访问"../TestResults/MyTest.csv"
到目前为止,我在每个测试夹具中都有以下常量:
private const string platformDependentPrefix = "";
Run Code Online (Sandbox Code Playgroud)
如果我运行x86的测试,我需要在每个测试夹具中手动将""改为"../".
有没有办法实现自动化?
我正在使用ReSharper来运行在C#/ NUnit中开发的单元测试.当我对C++/CLI类进行单元测试时,我的所有测试都会通过,但随后弹出以下消息框; "任务运行者应用程序已停止工作".我正在测试的类没有任何不受管理的东西:
public ref class MyClass
{
public:
MyClass(
array<double>^ rawPrices,
array<DateTime>^ priceDates)
{
// some unmanaged C++ code runs here
}
(snip)
~MyClass()
{
}
private:
int numDays;
array<double>^ Prices;
array<double>^ Discounts;
};
Run Code Online (Sandbox Code Playgroud)
当所有测试都成功时,会弹出消息.但是,当我切换到调试单元测试时,测试才成功.我正在测试的库是在Release x64模式下编译的.
这种奇特的行为是否表明MyClass有问题?
我正在开发一个围绕TryParse的通用包装器,如下所示:
public delegate bool ParseDelegate<T>(string s, out T result);
public static T? ParseOrNull<T>(this string value, ParseDelegate<T> parse) where T : struct
{
T result;
var parsed = parse(value, out result);
return parsed ? result : (T?)null;
}
[Test]
public void ParsesValidInt()
{
Assert.AreEqual(1234, "1234".ParseOrNull<int>(int.TryParse));
}
[Test]
public void ParsesValidDecimal()
{
Assert.AreEqual(12.34M, "12.34".ParseOrNull<decimal>(decimal.TryParse));
}
Run Code Online (Sandbox Code Playgroud)
这有点重复.有没有办法避免提及int.TryParse,所以我的调用如下所示:
"1234".ParseOrNull<int>()
Run Code Online (Sandbox Code Playgroud) 我需要将配置文件的多个部分转换为字典.这些词典的值有不同的类型.以下两个类可以工作,但它们几乎完全相同:
public class IntConfigSection
{
private static readonly ILog Log = LogManager.GetLogger(typeof(IntConfigSection));
public static Dictionary<String, int> LoadSection(string sectionName)
{
var ret = new Dictionary<String, int>();
try
{
var offsetsHash = (Hashtable)ConfigurationManager.GetSection(sectionName);
foreach (DictionaryEntry entry in offsetsHash)
{
ret.Add((String)entry.Key, int.Parse((String)entry.Value));
}
}
catch(Exception e)
{
Log.ErrorFormat("LoadSection:" + e);
}
return ret;
}
}
public class StringConfigSection
{
private static readonly ILog Log = LogManager.GetLogger(typeof(StringConfigSection));
public static Dictionary<String, String> LoadSection(string sectionName)
{
var ret = new Dictionary<String, String>();
try
{
var offsetsHash …
Run Code Online (Sandbox Code Playgroud) 我正在使用VS 2010,并使用NUnit运行我的单元测试.以下行正确检测两个列表是否不同:
CollectionAssert.AreEqual(expected, actual);
Run Code Online (Sandbox Code Playgroud)
但是,我想要一个比以下更好的错误消息:
Expected and actual are both <System.Collections.Generic.List`1[MyNamespace.MyClass]> with 2 elements
Values differ at index [0]
Expected: <MyNamespace.MyClass>
But was: <MyNamespace.MyClass>
Run Code Online (Sandbox Code Playgroud)
在MyNamespace.MyClass中,我实现了以下方法:
public new string ToString()
Run Code Online (Sandbox Code Playgroud)
我希望NUnit输出以下内容:
Expected and actual are both <System.Collections.Generic.List`1[MyNamespace.MyClass]> with 2 elements
Values differ at index [0]
Expected: <24 ounces of cold beer>
But was: <2.4 ounces of rotten tomatoes>
Run Code Online (Sandbox Code Playgroud)
但是,NUnit不会调用它.我错过了什么?
我们的生产服务器会终止非活动连接,因此我们的API需要在需要时恢复它们.以下代码有效,但它非常重复:
private const int MaxRetryCount = 3;
public static SqlDataReader RestoreConnectionAndExecuteReader(SqlCommand command)
{
int retryCount = 0;
while (retryCount++ < MaxRetryCount)
{
try
{
if (command.Connection.State == ConnectionState.Closed)
command.Connection.Open();
return command.ExecuteReader();
}
catch(Exception e)
{
if(!e.Message.ToLower().Contains("transport-level error has occurred"))
{
throw;
}
}
}
throw new Exception("Failed to restore connection for command:"+command.CommandText);
}
public static void RestoreConnectionAndExecuteNonQuery(SqlCommand command)
{
var retryCount = 0;
while(retryCount++ < MaxRetryCount)
{
try
{
if (command.Connection.State == ConnectionState.Closed)
command.Connection.Open();
command.ExecuteNonQuery();
return;
}
catch(Exception e)
{ …
Run Code Online (Sandbox Code Playgroud) c# ×7
refactoring ×3
unit-testing ×3
c#-4.0 ×2
nunit ×2
sql-server ×2
ado.net ×1
c++-cli ×1
generics ×1
linq-to-sql ×1
resharper ×1
t-sql ×1
templates ×1