Microsoft SQL Server Compact版本插入查询返回select@@identity

本文关键字:查询 返回 select@@identity 插入 版本 SQL Server Compact Microsoft | 更新日期: 2023-09-27 18:26:18

我正在尝试创建一个函数,该函数将一条记录插入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;
}

我做错了什么??

Microsoft SQL Server Compact版本插入查询返回select@@identity

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

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

@@识别