2

I'm trying to pull data from an SQL variable in C# to use in another SQL query.

Basically I have a for loop that is running through a datagrid and inserting the data into a table which I need to be linked to @DataID in this query below. As it is in a different query I can't access it so I want to pull it out into a var.

What's the best way to go about this? already searched lots of options and not coming up with anything that works

The help is appreciated!

Cheers

string dartBoxQuery = @"DECLARE @DataID int;
INSERT INTO DartBox (DartBoxNumber, ReturnDate, Comments)
VALUES (@dbn, @rtndate, @cmmts)
SELECT @DataID = scope_identity();";

// set up the command before exec
SqlCommand cmd = new SqlCommand(dartBoxQuery, con);

//set parameters
cmd.Parameters.AddWithValue("@rtndate", dateTimePicker1.Text);
cmd.Parameters.AddWithValue("@dbn", textBox1.Text);
cmd.Parameters.AddWithValue("@cmmts", textBox2.Text);

// call SQL connection
con.Open();
// execute above query
cmd.ExecuteNonQuery();

//close connection
con.Close();
11
  • return your result as DataSet(), Also, your query is not returning any it is just setting scope_identity into a @DataID varialbe Commented Apr 17, 2018 at 21:22
  • Stop using AddWithValue. Commented Apr 17, 2018 at 21:27
  • @KashifQureshi thanks - could you give me an example please? Really stuck and still learning, appreciated Commented Apr 17, 2018 at 21:30
  • @DourHighArch had no idea this was bad, thanks for this Commented Apr 17, 2018 at 21:32
  • you can also use OUTPUT INSERTED.ID INTO #Temp. And then SELECT * FROM #Temp Commented Apr 17, 2018 at 21:33

1 Answer 1

3

If you want to fetch @DataID back to the caller, there are 3 options:

  • declare an output parameter... presumably just moving @DataID to be an output parameter rather than a local variable; add an extra parameter and give it the direction of ParameterDirection.Output; after the ExecuteNonQuery, read out the value
  • at the end of your existing SQL, return @DataID; add an extra parameter and give it the direction of ParameterDirection.ReturnValue; after the ExecuteNonQuery, read out the value
  • at the end of your existing SQL, select @DataID; use ExecuteScalar and read out the return value

In this case, ExecuteScalar is probably the easiest option:

string dartBoxQuery = @"DECLARE @DataID int;
INSERT INTO DartBox (DartBoxNumber, ReturnDate, Comments)
VALUES (@dbn, @rtndate, @cmmts)
SELECT @DataID = scope_identity();

SELECT @DataID";

// set up the command before exec
SqlCommand cmd = new SqlCommand(dartBoxQuery, con);

//set parameters
cmd.Parameters.AddWithValue("@rtndate", dateTimePicker1.Text);
cmd.Parameters.AddWithValue("@dbn", textBox1.Text);
cmd.Parameters.AddWithValue("@cmmts", textBox2.Text);

// call SQL connection
con.Open();
// execute above query
var dataId = Convert.ToInt32(cmd.ExecuteScalar());

//close connection
con.Close();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks so much @Marcgravell this worked perfectly really helpful explaination. Now I can sleep not thinking about it and start on more tomorrow :))
@Andy00001 now if I may offer some advice: writing ADO.NET code by hand is painful and hard; I strongly recommend tools like "Dapper" for this purpose (I'm biased, note); your calling code becomes much easier!
Thank you I will definitely have a look at this!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.