简体   繁体   English

将自动递增的值插入到另一个表的列中?

[英]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. 但是,我处于绑定状态,因为在对数据库进行了一些必要的重组之后,我需要重新编写insert和select命令。 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. 我的提交表有一个自动递增的SubmissionId列,我需要将该值插入到Broker和Customer表的SubId列中。 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 + "', '" + 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. 查看DbCommand/SqlCommand.ExecuteScalar()以执行您拥有的第一个查询。

var id = cmd.ExecuteScalar(idQuery)

will get you the id of the last inserted auto id row. 将为您获取最后插入的自动ID行的ID。

I think you can use the value you get back to replace all *whatgoeshere?* 我认为您可以使用返回的值替换所有*whatgoeshere?*

The SqlCommand msdn page has a good example that you can refer to. SqlCommand msdn页上有一个很好的示例,您可以参考。

You would want to run this code first: 您可能想先运行以下代码:

SET IDENTITY_INSERT Customer ON;

Then you can run your INSERT statements. 然后,您可以运行INSERT语句。 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. 如果将所有这些都放入SP。 And then you can grab the subID from the last inserted submission table: 然后,您可以从最后插入的提交表中获取subID:


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

Now you can use @subID in subsequent queries.. 现在,您可以在后续查询中使用@subID。

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: 如果无法将其放入SP,则需要首先执行idQuery(使用SqlCommand.ExecuteScalar()),然后获取结果并将其保存到代码变量中,然后在后续查询中使用它。像这样的东西:


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? 我之所以说将所有内容都放入SP中,是因为它更易于维护,并且我认为从您的代码来看,您应该对整个事情有一个交易–您不希望客户查询起作用,然后经纪人查询失败,对吗? 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 您也可以从代码中使用数据库事务,但这要复杂一些: http : //www.knowdotnet.com/articles/transactions.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM