Tomas Westerlund

Tomas Westerlund

  • NA
  • 38
  • 31.8k

Get an OUTPUT variable from an Insert SQL statement

Nov 24 2012 1:53 PM
Hi,
I have been looking for away to get an output variable from an SQL query, however for SQL Compact Edition.

using (da.InsertCommand = new SqlCeCommand(@"INSERT INTO SCHEMAS (SCHEMA_NAME, FromMethod, ToMethod, ConnectedToUserID) OUTPUT INSERTED.SCHEMA_ID
                VALUES (@SchemaName, @FromMethod, @ToMethod, @ConnectedToUserID)", connString))
                {


                    da.InsertCommand.Parameters.AddWithValue("@SchemaName", textBoxSchemaName.Text);
                    da.InsertCommand.Parameters.AddWithValue("@FromMethod", comboBoxFromMethod.Text);
                    da.InsertCommand.Parameters.AddWithValue("@ToMethod", comboBoxToMethod.Text);
                    da.InsertCommand.Parameters.AddWithValue("@ConnectedToUserID", userID);


                    da.InsertCommand.Connection.Open(); //Needed to open a connection
                    da.InsertCommand.ExecuteNonQuery(); //Performs the actual SQL statement




                    this.Close();


                }



Can someone please assist my how I can get this output variable out to a C# variable?

Thanks in advance,
Tomas


Answers (2)