对数据库的操作出现了问题.

来源:百度知道 编辑:UC知道 时间:2024/05/06 10:16:20
asp.cs代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.Common;
using System.Data.SqlClient;

public partial class Vote_OptionModify : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string strSql="insert into VoteOptions(Vote_ID,VO_Text,VO_Point) values('"+Request.QueryString["vid"]+"','"+TextBox1.Text+"','0');";
string strConn="Data Source=hao-pc;Initial Catalog=vote;Persist Security Info=True;User

NSERT 语句与 FOREIGN KEY 约束"FK_VoteOptions_z_VoteInfo"冲突。该冲突发生于数据库"vote",表"dbo.z_VoteInfo", column 'Vote_ID'。

不知道你数据库了解多少,从上面的例子来看是数据库内部出现问题而这个错误是很常见的!

如果你有两个表

比如说

学生表,学生成绩表

学生表当然是记录学生的基本资料的

这个表里面有一个pkid也就是自动编号



学生成绩表里面有一个typeid绑定于学生表的自动编号(foreign key),这样做的好处就是没有存储重复的数据,并且可以快速的联系到学生资料!



假如说你的学生表里面有3个同学,那么自动编号就有3个,分别对应于每一个同学,假设自动编号的值分别是1,2,3,那么这个时候插入到学生成绩表的时候的那个typeid字段的值就只能是1,2,3的其中一个,否则就会如提示你的错误一样

我想我应该描述清楚了!

其实你可以删除掉约束,但是这样做的后果就是很有可能让你的数据对应出现混乱!

我给你写一个数据库的例子

--用户表以及用户blog表
create table userinfo
(
id int primary key identity(1,1),
name varchar(30) unique not null,
age tinyint check(age>0 and age<=120)
)
--为了简单,就只有一个文章标题,分类也去掉
create table blogs
(
id int primary key identity(1,1),
typeid int constraint myfk foreign key(typeid) referen