如何对数据库执行插入并使用Dapper返回插入的标识?
我尝试过这样的事情:
string sql = "DECLARE @ID int; " +
"INSERT INTO [MyTable] ([Stuff]) VALUES (@Stuff); " +
"SELECT @ID = SCOPE_IDENTITY()";
var id = connection.Query<int>(sql, new { Stuff = mystuff}).First();
Run Code Online (Sandbox Code Playgroud)
但它没有用.
@Marc Gravell谢谢你的回复.我已经尝试过你的解决方案但是,下面仍有相同的异常跟踪
System.InvalidCastException: Specified cast is not valid
at Dapper.SqlMapper.<QueryInternal>d__a`1.MoveNext() in (snip)\Dapper\SqlMapper.cs:line 610
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in (snip)\Dapper\SqlMapper.cs:line 538
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param) in (snip)\Dapper\SqlMapper.cs:line 456
Run Code Online (Sandbox Code Playgroud) 鉴于:
public interface IA
{
void TestMethod();
}
public interface IB : IA
{
}
Run Code Online (Sandbox Code Playgroud)
为什么:
typeof(IB).GetMethods().Count() == 0;
Run Code Online (Sandbox Code Playgroud)
?
只是要清楚:
public class A
{
public void TestMethod()
{
}
}
public class B : A
{
}
typeof(B).GetMethods().Count();
Run Code Online (Sandbox Code Playgroud)
确实有效(它返回5);
作为奖励:
typeof(IB).BaseType == null
Run Code Online (Sandbox Code Playgroud) 鉴于注册服务:
builder.RegisterType<Foo1>().Named<IFoo>("one").As<IFoo>();
builder.RegisterType<Foo2>().Named<IFoo>("two").As<IFoo>();
builder.RegisterType<Foo3>().Named<IFoo>("three").As<IFoo>();
Run Code Online (Sandbox Code Playgroud)
我可以IFoo
通过注入类似的东西来检索接口的命名实现Func<string, IFoo>
吗?
public class SomeClass(Func<string, IFoo> foo) {
var f = foo("one");
Debug.Assert(f is Foo1);
var g = foo("two");
Debug.Assert(g is Foo2);
var h = foo("three");
Debug.Assert(h is Foo3);
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以做到Meta<>
,但我不想使用它.
任何人都可以通过C#中的示例向我指出X509证书上的一个很好的介绍性材料.
我有3台造型机.一个运行在Windows 2000上,一个运行XP SP3,另一个运行64位Windows Server 2008.我有一个原生的C++项目要构建(我正在使用visual studio 2005 SP1构建).我的目标是使用这些构建机器"完全"构建相同的dll.
确切地说,我的意思是一点一点(当然除了构建时间戳).
有了win2k和winxp,我就得到了相同的dll.但它们与使用win2008服务器构建的dll不同.我已经设法得到几乎相同的dll,但有一些差异.在反汇编文件后,我发现函数顺序不一样(3个函数的顺序不同).
有谁知道这可能是什么原因?
还有一个问题:在vcbuild.exe中我找到了一个开关/ ORDER.以功能订单文件为输入.任何人都知道该文件应该是什么样的?
有没有办法更改WPF/Silverlight设计器(Cider)的默认背景?或者也许一些"IsInDesignMode"/可忽略的黑客这样做.
问题是,我的用户控件中有透明背景,我的文本大多是白色的(我的外壳很暗).而我在设计师中并没有看到他们.
支持asp.net 2.0-3.5 ajax Web应用程序数据的最佳实践是什么?我不想使用更新面板,只是纯文本数据(JSON).我应该使用网络服务吗?还是有另一种方式.
我已经了解到在sqlite中启用FK约束的唯一方法是打开这个pragma:
PRAGMA foreign_keys = ON;
Run Code Online (Sandbox Code Playgroud)
但不幸的是,如果我理解正确,我将不得不在每个会话开始时执行该查询.我对么?我怎么能用NHibernate"自动"做到这一点?有没有办法挂钩NH并在打开会话后立即执行?我正在使用NH 3.0.
c# ×4
.net ×2
ajax ×1
asp.net ×1
autofac ×1
c++ ×1
cider ×1
dapper ×1
designer ×1
dll ×1
foreign-keys ×1
inheritance ×1
interface ×1
linker ×1
nhibernate ×1
reflection ×1
security ×1
silverlight ×1
sql-server ×1
sqlite ×1
wpf ×1