English 中文(简体)
在服务器CEQ中添加新行的浏览和检索特征
原标题:Inserting a row and retrieving identity of new row in SQL Server CE

我试图插入一行,把新行的特征带回:

INSERT INTO blah....;
SELECT @@IDENTITY as NewID;

我试图在C#中单独援引一个DbCommand物体执行这两项声明......这似乎行不通,或者我错了。

我读到,《契约》一版并不支持以批号执行多项声明。

如果你希望同时提出多重询问,你必须包含每次发言的新线性,并在每次发言结束时有分界线。

资料来源:

因此,它的工作是不是......,如果是,我失踪了什么?

(我相信,我可以执行两条指挥,并做罚款,但我不知道我是否失踪了。)

最佳回答

这似乎是一种错误,而这是另一.。 你们只能按要求发言。

问题回答

你们必须确定产出参数,以获得身份价值

sqlCommand Command = Conn.CreateCommand();
Command.CommandText = "insert into MyTable(Value1,Value2) values(MyValue,MyValue)"
Command.ExecuteNonQuery();
Command.CommandText = "select @ID = @@IDENTITY"
SqlParameter ID = new SqlParameter("@ID", SqlDbType.Int);
ID.Direction = ParameterDirection.Output;
Command.Parameters.Add(ID);
Command.ExecuteNonQuery();
int NewID = (int)ID.Value;

在<代码>sql-server-ce上出现许多错误之后,例如“指示”可能不成产出,我可以这样做。

public static long GetIdentity(this IDbConnection connection)
{
    var cmd = connection.CreateCommand();
    cmd.CommandText = "SELECT @@IDENTITY";
    cmd.CommandType = CommandType.Text;
    var id = cmd.ExecuteScalar();
    return (long)(decimal)id;
}




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签