我知道我的问题很奇怪,但是我想使用C#使用For循环在Sql Server中插入数据。我有如下数据:
Insert into tblQuestions(Question, Description, Image, TopicId) values('Urogenital Diaphragm is formed by A/E', NULL, NULL, 1)
Insert into tblOptions(Option, QId, Answer) values('Colle''s fascia', NULL, 0)
Insert into tblOptions(Option, QId, Answer) values('Sphincter urethra', NULL, 1)
Insert into tblOptions(Option, QId, Answer) values('Perineal membrane', NULL, 0)
Insert into tblOptions(Option, QId, Answer) values('Deep perineal muslces', NULL, 0)
Insert into tblQuestions(Question, Description, Image, TopicId) values('The intricately and prodigiously looped system of veins and arteries that lie on the surface of the epididymis is known as:', NULL, NULL, 1)
Insert into tblOptions(Option, QId, Answer) values('Choroid plexus', NULL, 0)
Insert into tblOptions(Option, QId, Answer) values('Tuberal plexus', NULL, 0)
Insert into tblOptions(Option, QId, Answer) values('Pampiniform plexus', NULL, 1)
Insert into tblOptions(Option, QId, Answer) values('Pectiniform septum', NULL, 0)
这样,我有很多数据。第一行是问题,接下来的4行包含该问题的选项。插入第一个问题ID后,它将生成问题ID,将在所有4个选项中使用而不是NULL。
请帮我解决这个问题。我正在寻找一些想法或建议。我在记事本文件中包含了所有这些数据。
答案 0 :(得分:1)
作为替代方法,如果您能够创建存储过程,则可以声明变量并在插入后捕获新创建的ID,然后在每个后续的Insert语句中使用它?
DECLARE @QuestionId int
Insert into tblQuestions(Question, Description, Image, TopicId) values('Urogenital Diaphragm is formed by A/E', NULL, NULL, 1)
SELECT @QuestionId = SCOPE_IDENTITY();
Insert into tblOptions(Option, QId, Answer) values('Colle''s fascia', @QuestionId, 0)
Insert into tblOptions(Option, QId, Answer) values('Sphincter urethra', @QuestionId, 1)
Insert into tblOptions(Option, QId, Answer) values('Perineal membrane', @QuestionId, 0)
Insert into tblOptions(Option, QId, Answer) values('Deep perineal muslces', @QuestionId, 0)
Insert into tblQuestions(Question, Description, Image, TopicId) values('The intricately and prodigiously looped system of veins and arteries that lie on the surface of the epididymis is known as:', NULL, NULL, 1)
SELECT @QuestionId = SCOPE_IDENTITY();
Insert into tblOptions(Option, QId, Answer) values('Choroid plexus', @QuestionId, 0)
Insert into tblOptions(Option, QId, Answer) values('Tuberal plexus', @QuestionId, 0)
Insert into tblOptions(Option, QId, Answer) values('Pampiniform plexus', @QuestionId, 1)
Insert into tblOptions(Option, QId, Answer) values('Pectiniform septum', @QuestionId, 0)
答案 1 :(得分:0)
我要告诉你一些会改变你生活的事情。 :)好吧,不是真的,但是在编写查询等时会更容易。
下载数据访问块here。
然后将其添加到您的项目资源中并包含。
您要输入以插入表格等的唯一代码是:
//Using:
using Microsoft.ApplicationBlocks.Data;
//Setting connection
private static string _connection = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
//sending info to a stored proc (Which is better) in my opinion:
SqlHelper.ExecuteNonQuery(_connection, "usp_AddUpdateTargets", week1target, week2target, week3target, week4target, UserID, TerritoryID);
这就是它的全部内容。显然你必须得到你的变量等。
希望这有帮助。
答案 2 :(得分:0)
从查询性能的角度来看,调用数据库插入单行是不好的。我想建议for循环连接查询作为带分隔符的查询字符串&#34 ;;"最后一次执行整个插入语句。例如:
字符串查询;
for循环(singleInsertQuery){ query = query +";" + singleInsertQuery }
执行(查询);