开发者

Insert auto incremented value into another table's column?

I'm building a practice project for training, and my handler has forbid me to parameterize, preferring that I focus on other things at the moment. He's instructed me to use the following types of strings to insert. I KNOW it's not safe. It's not for actual deployment. I'm in a bind, however, because after doing some necessary restructuring to the database, I need to re write the insert and select commands. My Submission table has an auto-incremented SubmissionId column, and I need to insert that value into the SubId columns of my Broker and Customer tables. How do I do this?

string idQuery = "SELECT SCOPE_IDENTITY() AS LastInsertedSubmissionId";

String custQuery = "INSERT INTO Customer 
                      (CustId, CustName, SicNaic, CustAdd, CustCity, CustState, CustZip, SubId)           
                    VALUES
                      ('" + TbCustId.Text + "', '" + TbCustName.Text + "', '" + RblSicNaic.SelectedItem + "', '" + TbCustAddress.Text + "', '" + TbCustCity.Text + "', '" + DdlCustState.SelectedItem + "', '开发者_JAVA百科" + TbCustZip.Text + "', *whatgoeshere?*)";
String broQuery = "INSERT INTO Broker 
                     (BroId, BroName, BroAdd, BroCity, BroState, BroZip, EntityType, SubId) 
                   VALUES 
                     ('" + TbBroId.Text + "', '" + TbBroName.Text + "', '" + TbBroAddress.Text + "', '" + TbBroCity.Text + "', '" + DdlBroState.SelectedItem + "', '" + TbBroZip.Text + "', '" + DdlEntity.SelectedItem + "', *whatgoeshere?*)";

String subQuery = "INSERT INTO Submission 
                     (Coverage, CurrentCoverage, PrimEx, Retention, EffectiveDate, Commission, Premium, Comments) 
                   VALUES 
                     ('" + TbCoverage.Text + "','" + TbCurrentCoverage.Text + "','" + TbPrimEx.Text + "','" + TbRetention.Text + "','" + TbEffectiveDate.Text + "','" + TbCommission.Text + "','" + TbPremium.Text + "','" + TbComments.Text + "')";


Look into DbCommand/SqlCommand.ExecuteScalar() for executing the first query you have.

var id = cmd.ExecuteScalar(idQuery)

will get you the id of the last inserted auto id row.

I think you can use the value you get back to replace all *whatgoeshere?*

The SqlCommand msdn page has a good example that you can refer to.


You would want to run this code first:

SET IDENTITY_INSERT Customer ON;

Then you can run your INSERT statements. After you are done, run this statement:

SET IDENTITY_INSERT Customer OFF;


First of all, I would possibly make this into a stored procedue, if you can. If you put all of this into a SP. And then you can grab the subID from the last inserted submission table:


declare @subID int
set @subID = (SELECT SCOPE_IDENTITY() AS LastInsertedSubmissionId)

Now you can use @subID in subsequent queries..

If you cannot make this into a SP, then you need to execute the idQuery first (using SqlCommand.ExecuteScalar()), and then get the results and save it into your code variable.. and then use that in your subsequent queries.. something like this:


var subID = cmd.ExecuteScalar(idQuery) 

The reason I am saying to put everything in a SP is because it is easier to maintain, and I think judging by your code, you should have a trasaction around the whole thing - you don't want the customer query to work, and then the broker query to fail, correct? You will lose data integrity, since now you will have an extra row in your customer table.

You can also use DB transactions from your code, but it is a little more complicated: http://www.knowdotnet.com/articles/transactions.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜