Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用c#和Microsoft Access插入的最后一个ID_C#_Sql_Ms Access - Fatal编程技术网

使用c#和Microsoft Access插入的最后一个ID

使用c#和Microsoft Access插入的最后一个ID,c#,sql,ms-access,C#,Sql,Ms Access,如何插入最后一个ID?我需要子表的新ID。 谢谢。尝试使用两个OLEDB命令,一个用于INSERT语句,另一个用于SELECT@@IDENTITY语句。使用第二个OLEDB命令,在一个OLEDB连接中,您可以使用ExecuteOnQuery运行第一个INSERT,然后使用ExecuteCalar运行SELECT@@IDENTITY。你可以。分析结果以获得新id public bool InserisciLibro(LibroCLS libro) { bool isSucc

如何插入最后一个ID?我需要子表的新ID。
谢谢。

尝试使用两个OLEDB命令,一个用于INSERT语句,另一个用于SELECT@@IDENTITY语句。使用第二个OLEDB命令,在一个OLEDB连接中,您可以使用ExecuteOnQuery运行第一个INSERT,然后使用ExecuteCalar运行SELECT@@IDENTITY。你可以。分析结果以获得新id

public bool InserisciLibro(LibroCLS libro)
    {
        bool isSuccess = false;
        using (OleDbConnection con = new OleDbConnection(myConnectionstring))
        {
            con.Open();
            string SQL =@"INSERT INTO tblLibri (AutoreFK, Titolo,   Pagine,  EditoreFK, ISBN,   Notes) VALUES (@AutoreFK, @Titolo, @Pagine, @EditoreFK, @ISBN, @Notes);";
            int idnewlibro = 0;
            using (OleDbCommand cmd = new OleDbCommand(SQL, con))
            {
                cmd.Parameters.AddWithValue("@AutoreFK",libro.AutoreFK);
                cmd.Parameters.AddWithValue("@Titolo", libro.Titolo);
                cmd.Parameters.AddWithValue("@Pagine", libro.Pagine);
                cmd.Parameters.AddWithValue("@EditoreFK", libro.EditoreFK);
                cmd.Parameters.AddWithValue("@ISBN", libro.ISBN);
                cmd.Parameters.AddWithValue("@Notes", libro.Notes);
                int row = cmd.ExecuteNonQuery();

                //ora lancio il cmd per ottenere l'id autoincrementale ottenuto
                OleDbCommand cmdIdentity = new OleDbCommand("SELECT @@IDENTITY", con);                    
                idnewlibro =(int) cmdIdentity.ExecuteScalar();
                if (idnewlibro != 0)
                {
                    isSuccess = true;
                }                   
            }
        }
        return isSuccess;
    }

这很有效。谢谢