繁体   English   中英

Microsoft SQL Server Compact Edition插入查询返回选择@@ identity

[英]Microsoft SQL Server Compact edition insert query return select @@identity

我正在尝试创建一个函数,该函数将一条记录插入到Microsoft SQL Server Compact版本3.5数据库中,并返回新插入的行的ID。

这是我的代码:

upit = "insert into Crtez (Ncrteza, ProjID, lilu, lide, dubinaRova, d1, d2, ZCdulina, ZCpromjer, dBusenja) 
        values ('primjer 2 rolaza.dwg', 3, 49192.62, 49222.62, 3.11, 74.7693403335958, 15.2495262383346, 14,0,0.00)";

public static int UpisiUBazu(String putanja, String upitUpisa)
{
    int id = default(int);
    SqlCeConnection con = new SqlCeConnection();

    try
    {
        String constring = "Data Source=" + putanja;

        using (con = new SqlCeConnection(constring))
        {
            con.Open();
            SqlCeTransaction tr = con.BeginTransaction();
            SqlCeCommand com = null;
            com = new SqlCeCommand(upitUpisa, con);
            com.Transaction = tr;
            com.ExecuteNonQuery();
            com = new SqlCeCommand(@"SELECT @@IDENTITY AS ID", con);
            object o = com.ExecuteScalar();
            id = Convert.ToInt32(o);
        }
    }
    catch (SqlCeException ex)
    {
        MessageBox.Show("Pojavila se greška: " + ex.Message + "/" + ex.NativeError + "/" + ex.InnerException);
    }
    finally
    {
        con.Close();
    }
    return id;
}

我究竟做错了什么??

SELECT @@IDENTITY AS ID应该与INSERT语句在同一查询中。

INSERT INTO Crtez (...) VALUES (...); SELECT @@IDENTITY AS ID

@@ IDENTITY

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM