2
Answers

Get an OUTPUT variable from an Insert SQL statement

Tomas Westerlund

Tomas Westerlund

11y
2.3k
1
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)