|
Yes, that's the whole point. That is what an IDENTITY field in SQL Server is.
And when you use autoincremented fields you can't rely on the MAX value to be the one you inserted: you do your INSERT, then two dozen other users add their own before you issue your SELECT. YOur MAX then fetches the ID for a totally different row, and you start to get intermittent problems that are fiendishly difficult to duplicate, track down, fix, and prove.
Using SCOPE_IDENTITY returns the last autoincremented value used by that connection, and will never return values from different users.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OK well sir and I just found her in my research, are I adapting well to my code?
using (OleDbCommand cmd1 = sql_con.CreateCommand ())
{
cmd1.CommandText = "INSERT INTO Orders (amount_com) VALUES (@montant_com)";
cmd1.CommandText = "SELECT @@ IDENTITY";
int id = Convert.ToInt32 (cmd1.ExecuteScalar ());
cmd1.Parameters.AddWithValue ("@ amount_com", TxtTotalCmd.Text);
cmd1.ExecuteNonQuery ();
}
|
|
|
|
|
Stop guessing. Start thinking.
Look at your code and tell me what is wrong with it.
Where have we seen that problem before?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Ok my apologies, this is the error I get
System.Data.Oledb.Ole Exception (0x80040E14): Character found after the end of the Sql instruction
using (OleDbCommand cmd1 = sql_con.CreateCommand())
{
cmd1.CommandText = "INSERT INTO Commandes (montant_com) VALUES (@montant_com);" + "SELECT SCOPE_IDENTITY()";
int id;
cmd1.Parameters.AddWithValue("@montant_com", TxtTotalCmd.Text);
cmd1.ExecuteNonQuery();
id = (int)cmd1.ExecuteScalar();
TxtNunCmd.Text = id.ToString();
}
|
|
|
|
|
That's better!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OK, the edited version makes a little more sense.
You need to split your command into two: Do the INSERT, then build a new Command to do the SELECT (some DB engines don't like command chaining). As long as you use the same connection and you don't close it in between the INSERT and SELECT, you'll be fine.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I just did it but I have an error: the function 'SCOPE_IDENTITY' not defined in the expression
using (OleDbCommand cmd1 = sql_con.CreateCommand())
{
cmd1.CommandText = "INSERT INTO Commandes (montant_com) VALUES (@montant_com)";
cmd1.CommandText = "SELECT SCOPE_IDENTITY()";
cmd1.Parameters.AddWithValue("@montant_com", TxtTotalCmd.Text);
cmd1.ExecuteNonQuery();
int id = Convert.ToInt32(cmd1.ExecuteScalar());
TxtNunCmd.Text = id.ToString();
}
|
|
|
|
|
and I wanted to clarify that I am on an access database
|
|
|
|
|
Ah. That makes a difference.
Access doesn't support SCOPE_IDENTITY: You need to use @@IDENTITY instead.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
it’s already done sir but what I don't understand textbox does not recover the id, it displays zero
using (OleDbCommand cmd1 = sql_con.CreateCommand())
{
cmd1.CommandText = "INSERT INTO Commandes (montant_com) VALUES (@montant_com)";
cmd1.CommandText = "SELECT @@IDENTITY";
cmd1.Parameters.AddWithValue("@montant_com", TxtTotalCmd.Text);
cmd1.ExecuteNonQuery();
id = (int)cmd1.ExecuteScalar();
TxtNunCmd.Text = id.ToString();
}
|
|
|
|
|
Do you know what the wonderful thing about banging your head on the desk is?
Desk *BANG*
Desk *BANG*
Desk *BANG*
Desk *BANG*
Desk *BANG*
Desk *BANG*
It feels wonderful when you stop ...
We've been here before. Yesterday in fact.
Look at your code. What can you see that is wrong?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
:doh: :doh: :doh: :doh: you are right sir and I promise you find the solution and you ride it
|
|
|
|
|
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
it probably is too early to stop.
|
|
|
|
|
You had to say that, didn't you?
Desk *BANG*
Desk *BANG*
Desk *BANG*
...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
yes yes sir I am still looking for, I will find it, I assure you sir
|
|
|
|
|
using (OleDbCommand cmd1 = sql_con.CreateCommand())
{
cmd1.CommandText = "INSERT INTO Commandes (montant_com) VALUES (@montant_com)";
OleDbCommand cmr = sql_con.CreateCommand();
cmr.CommandText = "SELECT @@IDENTITY AS LastId";
cmd1.Parameters.AddWithValue("@montant_com", TxtTotalCmd.Text);
id = (int)cmr.ExecuteScalar();
TxtNunCmd.Text = id.ToString();
cmd1.ExecuteNonQuery();
}
but i can't get the id
|
|
|
|
|
Stop guessing, and start thinking.
When is the ID actually created? What line of code causes that to happen?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hello sir and all my excuses for yesterday I had a connection problem that's why I disconnected but by dint of fighting all night I could find the solution regaré and tell me if the code is clean. I must say I am glad I did ...
using (OleDbCommand cmd1 = sql_con.CreateCommand())
{
cmd1.CommandText = "INSERT INTO Commandes (montant_com) VALUES (@montant_com)";
cmd1.Parameters.AddWithValue("@montant_com", TxtTotalCmd.Text);
cmd1.ExecuteNonQuery();
OleDbCommand cmr = sql_con.CreateCommand();
cmr.CommandText = "SELECT @@IDENTITY AS LastId";
int LastId = Convert.ToInt32(cmr.ExecuteScalar());
TxtNunCmd.Text = LastId.ToString();
}
|
|
|
|
|
Yes, that's fine - you don't need the "AS LastID" because ExecuteScalar doesn't return labels, but it doesn't matter if you add it.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi guys i really need help. Any one Know how to set and marked ontime,late,and absence in sqldatabas help plss
|
|
|
|
|
Yes. I do, and so do most other people here.
Glad we could resolve that for you.
Next time, if you want to ask a question, try to make it rather more specific about your actual problem, and tell us what you have tried, where you are stuck, and what help you need. Remember that we can't see your screen, access your HDD, or read your mind - and that we aren't here to do your homework for you!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Bonsoir les amis veuillez m’excuser mais un problème pour recéper une info dans une requete sql.
Good evening friends please excuse me but a problem to receive information in a sql request.
try
{
setConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
string CommandText = "INSERT INTO Commandes (montant_com) VALUES ('" + TxtTotalCmd.Text + "')";
ExecuteQuery(CommandText);
long numCmd;
string CommandText1 = "SELECT MAX(num_com) AS dernier_num FROM Commandes";
sql_cmd = new OleDbCommand(CommandText1, sql_con);
numCmd = sql_cmd.Parameters.Add("dernier_num"); (j'ai une erreur à ce niveau)
}
[edit]Google Translate added - OriginalGriff[/edit]
|
|
|
|
|
Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.
When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood' The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable; Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x'; A perfectly valid SELECT
DROP TABLE MyTable; A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.
So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
And when you've fixed that throughout you app, start looking at the problem you have noticed.
And that is probably in your ExecuteQuery method: if you are creating an SQL Reader ther, then you can't issue any more commands on that connection until the Reader is closed...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
merci pour votre réponse je vais revoir mon code et je vous reviens
|
|
|
|