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#。望大哥大姐们多多指教。
(
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#。望大哥大姐们多多指教。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货