如何在sql数据库中插入数据? [英] How to insert data in sql data base?

查看:194
本文介绍了如何在sql数据库中插入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!
帮助!!!我正在使用c#,并且我想使用c#将数据添加到数据库表中
因为我有两个与表有关系的表
例如:

Hello!
help!!! i''m using c# and i want to add a data into my database tables using c#
since i have two tables having a relationship with tables
example:

table one column:did,dna,ddef,dsymp,dcompl;
table 2   column:syid,did,synam


然后我希望用户仅插入表1的所有列和表2的所有列,但做到这一点是外键,并且一旦插入表1,该如何使用我的代码重新插入外键,而又不要求用户重复??
谢谢您的建议"


then i want the user only inserts all column of table one and all column of table 2 except did which is foreign key and once inserted in table one then how can i re insert the foreign key using my code with out asking the user to isert repetedly??
" thanks for advice"

推荐答案

我认为您最好使用存储过程,例如:
I think you best use a stored proedure like:
create proc p_Insert
(
	@dna <type>,
	@ddef <type>,
 	@dsymp <type>,
	@dcompl <type>,
	@synam <type>
)
as
declare @did int
declare @syid int
insert into t1 (dna,ddef,dsymp,dc) values(@dna,@ddef,@dsymp,@dc)
set @did = scope_identity()
insert into t2 (did, synam) values(@did @synam)
set @syid = scope_identity()

select did = @did, syid= @syid

最后一次选择的查询将把新的id返回给您的程序

The last select query will return the new id''s to your program.


保存在第一个表中之后,执行插入"命令以在第二个表中添加值为"did"的新记录.然后,通过在"where子句"中使用"did"的值,对特定记录使用"update"命令获取用户值.
After saving in the first table, execute an ''insert'' command to add a new record in the second table with value of ''did''. Then use ''update'' command, for the user values, on the particular record, by using value of ''did'' in the ''where clause''.


这篇关于如何在sql数据库中插入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆