数据未保存在数据库中,没有错误。 [英] Data is not saving in database, no error.
问题描述
第一张表
1st table
CREATE TABLE [dbo].[Department] (
[Dept_ID] INT IDENTITY (10, 10) NOT NULL,
[Dept_Name] VARCHAR (50) NOT NULL,
CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED ([Dept_ID] ASC),
CONSTRAINT [FK_Department_Employee] FOREIGN KEY ([Dept_ID]) REFERENCES [dbo].[Department] ([Dept_ID])
);
第二桌
2nd table
CREATE TABLE [dbo].[Employee] (
[Dept_ID] INT NOT NULL,
[Dept_Name] VARCHAR (50) NOT NULL,
[Emp_ID] INT IDENTITY (1, 1) NOT NULL,
[Emp_Name] VARCHAR (50) NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ([Emp_ID] ASC)
);
代码 -
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System .Web.UI;
使用System.Web.UI.WebControls;
使用System.Configuration;
使用System.Data;
使用System.Data.SqlClient;
命名空间Data_Grid1
{
public partial class Employee:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
}
protected void btnSubmit_Click(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [constr] .ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(插入员工(Dept_ID,Dept_Name,Emp_Name)从部门WHERE Dept_Name = @Dept_Name中选择Dept_ID,Dept_Name,@ Emp_Name, con);
cmd.Parameters.AddWithValue(@ Emp_Name,txtName.Text.Trim());
cmd.Parameters.AddWithValue(@ Dept_Name, ddlDeptName.SelectedValue);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
code-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Data_Grid1
{
public partial class Employee : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Insert into Employee (Dept_ID, Dept_Name, Emp_Name) Select Dept_ID, Dept_Name, @Emp_Name from Department WHERE Dept_Name = @Dept_Name", con);
cmd.Parameters.AddWithValue("@Emp_Name", txtName.Text.Trim());
cmd.Parameters.AddWithValue("@Dept_Name", ddlDeptName.SelectedValue);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
推荐答案
试试这个。
Try this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace Data_Grid1
{
public partial class Employee : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
string Dept_Name = ddlDeptName.SelectedItem.Text;
SqlCommand cmd = new SqlCommand("Insert into Employee (Dept_ID, Dept_Name, Emp_Name) Select Dept_ID, Dept_Name, @Emp_Name from Department WHERE Dept_Name = '"+ Dept_Name + "'", con);
cmd.Parameters.AddWithValue("@Emp_Name", txtName.Text.Trim());
cmd.ExecuteNonQuery();
con.Close();
}
}
}
cmd.Parameters.AddWithValue("@Dept_Name", ddlDeptName.SelectedItem.Text);
试试这个..
Try This..
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("Insert into Employee (Dept_ID, Dept_Name, Emp_Name) Select Dept_ID, Dept_Name,'" + txtName.Text.Trim() + "' from Department WHERE Dept_Name = '"+ ddlDeptName.SelectedItem.Value +"'", con);
cmd.ExecuteNonQuery();
con.Close();
}
这篇关于数据未保存在数据库中,没有错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!