create table DeptInfo
(
 DeptId int identity(1,1) primary key,
     Deptname varchar(50)
)insert into DeptInfo values('人事部')
insert into DeptInfo values('市场部')
insert into DeptInfo values('人力资源部')create table Userinfo
(
UserID int identity(1,1) primary key,
UserName varchar(50) not null,
Sex char(2),
Age int,
Hobby varchar(50),
DeptID int references DeptInfo(DeptId)

)insert into Userinfo values('张三','男','18','篮球',1)
insert into Userinfo values('李四','女','19','篮球',2)
insert into Userinfo values('王二','男','20','篮球',3)
insert into Userinfo values('麻子','男','23','篮球',2)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace pro
{
    public partial class Form1 : Form
    {
        DBconnention db = new DBconnention();
        private SqlConnection conn = null;        public Form1()
        {
            InitializeComponent();
        }
        public void gettall()
        {
            this.cbbm.Items.Add("人事部");
            this.cbbm.Items.Add("市场部");
            this.cbbm.Items.Add("人力资源部");
            this.cbbm.DropDownStyle = ComboBoxStyle.DropDownList;
            this.cbbm.SelectedIndex = 0;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.gettall();
        }        private void button1_Click(object sender, EventArgs e)
        {
            conn = db.gett();
            string name = this.txtname.Text;
            string sex = this.radman.Checked ? radman.Text : radwoman.Text;
            string age = this.txtage.Text;
            string hobby = this.txthobby.Text;
            string cbbm = this.cbbm.TabIndex.ToString();
            string sql = "insert into Userinfo values(@name,@sex,@age,@hobby,@deptid) ";
            try
            {
                conn.Open();
                SqlCommand comm = new SqlCommand(sql, conn);
                comm.Parameters.AddWithValue("@name", name);
                comm.Parameters.AddWithValue("@sex", sex);
                comm.Parameters.AddWithValue("@age", age);
                comm.Parameters.AddWithValue("@hobby", hobby);
                comm.Parameters.AddWithValue("@deptid", cbbm);
                int a = comm.ExecuteNonQuery();
                if (a > 0)
                {
                    MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {                MessageBox.Show("你错误的原因是:" + ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }   }
}/////在C#中添加成员到Userinfo 表中会出现主键与外键的冲突,我想添加成功了是不是在Userinfo 中最后出现的部门是个数字呢? 要是数字那该怎么写?求解答、菜鸟刚学C#。望大哥大姐们多多指教。

解决方案 »

  1.   

    string sql = "insert into Userinfo values(@name,@sex,@age,@hobby,@deptid) ";应该是在你进行上面的插入操作过程中,@deptid在表DeptInfo中没有对应的值,不能完成操作,受到了主外键的约束,依照你上面建立的表结构和数据,现在在Userinfo中你只能插入@deptid=1或者=2或者=3的行记录
      

  2.   

    Userinfo表有DeptID外键,是个INT型的ID, string cbbm = this.cbbm.TabIndex.ToString();你穿进去的是个string,传进去的DeptID并需是DeptInfo表中存在的数据
      

  3.   

    有主外键约束。  Foreign Key
      

  4.   

    一楼的姐姐 我知道是插入数字 但是我在窗体应用中DeptInfo这个表中建的是COMBOBOX控件啊、下托就是这三个部门、不是数字,所以插不进去数字 我就纠结了、 呵呵 、