如何设置外键 [英] how to set foreign key

查看:104
本文介绍了如何设置外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张桌子艺术家和艺术家。 artistId是artist表中的主键,它是arts table中的外键。我使用存储过程来插入数据。第一个艺术家登录然后艺术信息页面显示艺术家插入艺术信息。我希望艺术家ID也插入艺术表插入艺术信息。我试着这样做:

存储过程:

如果存在(从艺术家中选择artistId)

插入arts(name,description,artistId)值(@ name,@ description,@ artistId)



asp.net代码:



 cmd.Parameters.AddWithValue(  @ name,txtname。 Text ); 
cmd.Parameters.AddWithValue( @ category,ddlcategory.SelectedValue);



它不工作。

我是存储过程的新手。请帮我获取外键值。还有其他方法吗?

解决方案

您的存储过程中有3个参数,需要设置所有参数值。

当艺术家登录系统时,您可以从数据库中获取artistId并保留该值。



 cmd.Parameters.AddWithValue(  @名,txtname.Text); 
cmd.Parameters.AddWithValue( @ category,ddlcategory.SelectedValue);
cmd.Parameters.AddWithValue( @ artistId,artistId);


I have two tables artist and arts. artistId is a primary key in artist table and it is the foreign key in arts table.I am using stored procedure to insert data. first artist log in and then art info page is displayed where artist insert art info.I want that artist id to get inserted in arts table also when he inserts art info.I tried this as:
stored procedure:
if exists(select artistId from artist)
insert into arts(name,description,artistId) values(@name,@description,@artistId)

asp.net code:

cmd.Parameters.AddWithValue("@name",txtname.Text);
        cmd.Parameters.AddWithValue("@category",ddlcategory.SelectedValue);


It is not working.
I am new to stored procedure.Please help me to get the foreign key value.Or is there any other way?

解决方案

you have 3 parameters in your stored procedure, need to set all the parameter values.
when artist login to system you can get the artistId from the database and keep that value.

cmd.Parameters.AddWithValue("@name",txtname.Text);
cmd.Parameters.AddWithValue("@category",ddlcategory.SelectedValue);
cmd.Parameters.AddWithValue("@artistId",artistId);


这篇关于如何设置外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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