I am new to C# and definitely new to Dapper.NET. This is the first time I'm tackling ORMs and frameworks so I would like to know more about some syntax.
I've seen several INSERT
queries and noticed that all have this
SELECT CAST(SCOPE_IDENTITY() AS INT);
after the INSERT INTO...;
line.
I would like to know the importance, or use of this. Thank you
Edit: here is some code I've written, and is working as far as I have tested.
This is the Users.cs
:
public class Users
{
private int _uid;
private string _fullname;
private string _username;
private string _passcode;
public int uid
{
get { return _uid; }
set { _uid = value; }
}
public string fullname
{
get { return _fullname; }
set { _fullname = value; }
}
public string username
{
get { return _username; }
set { _username = value; }
}
public string passcode
{
get { return _passcode; }
set { _passcode = value; }
}
}
and this is the insertUser.cs
:
public Users InsertData(Users newUser)
{
string strQry = @"INSERT INTO [Users] VALUES (@uid, @fullname, @username, @passcode);
SELECT * FROM [Users] WHERE [uid] = CAST(SCOPE_IDENTITY() as int);";
using(var conn = InitOpenConn())
{
try
{
var ins = conn.Query<Users>(strQry, newUser).Single();
return ins;
}
catch (Exception ex)
{
throw ex;
}
}
}
and this is Program.cs
:
InsertUser ium = new InsertUser();
try
{
Users newUser = new Users
{
uid = 10,
fullname = "James Bond",
username = "james007",
passcode = "095e3cbaf34467fbe1b0f4cd846a57460718d2b4ca504caca28ae5287e8d076db3ddadac11db87ab6ac9dac4b8d84d9a65e67e98ab6ef979c61f277f51df36f3"
};
Users insUsr = ium.InsertData(newUser);
refreshTable();
MessageBox.Show("Added !");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
When you insert a new row into a SQL Server table with an Identity
column, the ORM needs to get the generated ID from the database in order to update your model directly in the application.
Example:
/****************** pseudo code ******************/
SomeClass obj = new SomeClass();
obj.SomeProperty = someValue;
// INSERT TO database with Dapper and return inserted ID
int insertedID = SomeMethodToInsertUsingDapper(obj);
obj.ID = insertedID;
// The generated ID is get from the database using
// SELECT CAST(SCOPE_IDENTITY() AS INT);
// so this next line prints the ID generated in the database.
Console.WriteLine(obj.ID);
Dapper Limitations:
Dapper has some limitation on the Identity Column, So people using Dapper
use this SQL syntax SELECT CAST(SCOPE_IDENTITY() AS INT)
to get the generated ID
from the database
. And then use that ID
in the application
.
Identity Column in a numeric column in the database that gets incremented automatically on each insert.
SELECT CAST(SCOPE_IDENTITY() AS INT)
This line of code returns the last inserted identity ID of the table.
We have many use of this. Like we checked value inserted or not and also if you want some perform some operation on current inserted current.
If you are using EF(ORM) then and you table is EMp_master then you can get current inserted ID using below syntax
EMp_master obj=new Emp_master
obj.first="suraj";
obj.Emp_master.add(obj);
obj.savechagnes();
int currentinsertedid = obj.empid // this syntax same as SELECT
CAST(SCOPE_IDENTITY() AS INT) both give same record.