Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
.NET and @@identity
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
00666202
Message ID:
00666377
Vues:
27
CommandType can be Text, StoredProcedure or TableDirect. I don't remember what table direct does.
We may be doing things a little differently but here is a code sample of how I've been doing things to get my new PK back. (CommandType set to stored procedure):
	string sCommand = "usp_ClientAdd" ;
	SqlConnection oConnection = new SqlConnection("SERVER=MySqlServer;UID=MyUn;PWD=MyPw;DATABASE=MyDatabase") ;
	SqlCommand oCommand = new SqlCommand("",oConnection) ;
	SqlDataAdapter oAdapter = new SqlDataAdapter(oCommand) ;
	oCommand.CommandType = CommandType.StoredProcedure ; 
	oCommand.CommandText = sCommand ; 


	SqlParameter parmcName = oCommand.Parameters.Add("@cName", SqlDbType.VarChar);
	parmcName.Value = "My New Client"  ;


	// --- Set the primary key parameter
	SqlParameter parmsiPK = oCommand.Parameters.Add("@iPK", SqlDbType.Int);
	parmsiPK.Value = -1 ;
	parmsiPK.Direction = ParameterDirection.Output  ;

	// --- Run the command 
	oConnection.Open();
	oCommand.ExecuteNonQuery() ; 
	oConnection.Close();   
	
	MessageBox.Show("The New PK is " + parmsiPK.Value.ToString())  ; 
The SQL server stored procedure would look something like this:
CREATE PROCEDURE usp_ClientAdd
	@cName VarChar(40),
	@iPK int OUTPUT

AS
INSERT INTO [Client]
	(cName)
VALUES
	(@cName)

SET @iPK = @@IDENTITY
>I don't know what you mean by commandtype is text. Do you see from my info here that I am using commandtype text? If so, ident_current('dealer') is working for me though @@identity and scope_identity are not.
>
>Can you describe what commandtype=text means and the other possibilities?
>
>>Terry,
>>I have not found a way to get a PK when the commandtype is text. For what it's worth,I ended up making my insert routines stored procedures and specifying the PK as an output parameter. Not what I wanted to do in all cases but I couldn't find another way.
>>
>>I'll be interested to see if someone else has found a way to pull it off when commandtype is text.
>>
>>Chris
>>
>>
>>>Query looks like "insert into dealer(name, code, username, password, agent) values
>>>('X','Y','test','test',0)
>>>
>>>Dealer is table, and I'm not using Stored Procedures, but running first the insert statement and then immediately running the identity select in an ASP .NET session with a primary key field ID that is an identity with seed 1 increment 1.
>>>
>>>ds=sqlData.runsql("select @@identity from dealer","dealer"), doesn't work
>>>ds=sqlData.runsql("select scope_identity() from dealer","dealer"), doesn't work
>>>ds=sqlData.runsql("select ident_current('dealer')","dealer"), does work
>>>Then I use:
>>>dt=ds.tables(0)
>>>dim idy as integer = dt.rows(0)(0) to get the value and return it.
>>>The select used fill method of the dataadapter object.
>>>The insert uses executenonquery method of the command object.
>>>I understand somewhat the scope and session differences between these keywords, but don't understand why in .NET two don't work and the one does.
>>>
>>>
>>>
>>>Mike, have you seen the article in ASP Pro .NET by Dino Esposito on the SQLClient classes where he says most of the advantage of using Stored Procedures is gained by using these drivers vs Oledb?
>>>
>>>>You've not provided enough information. What's the query look like? What is "Dealer?" A table or stored procedure?
>>>>
>>>>I cannot think of any reason that ADO .NET would not be able to deal with @@IDENTIY?
>>>>
>>>>>.NET datatable doesn't return identity.
>>>>>
>>>>>ds=sqlData.runsql(strSql,"Dealer")
>>>>>dt=ds.tables(0)
>>>>>dim idx as integer = dt.rows(0)(0)
>>>>>return idx
>>>>>
>>>>>idx is dbnull always with .NET
>>>>>
>>>>>If I run the query in Query Analyzer, I get the proper id# for my auto-increment primary key id field right after running an INSERT command.
>>>>>
>>>>>Does .NET dataset not support the @@identity?
>>>>>
>>>>>How would I get the last created id?
>>>>>
>>>>>Thanks,
Chris
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform