我使用C#和sql在ASP.NET MVC 4中工作。
我有两张桌子:
CREATE TABLE [dbo].[Mem_Basic] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Mem_NA] VARCHAR (100) NOT NULL,
[Mem_Occ] VARCHAR (200) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
和
CREATE TABLE [dbo].[Mem_Details] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Mem_Id] INT NOT NULL,
[Mem_Role] VARCHAR (250) NOT NULL,
[Mem_Email] VARCHAR (50) NULL,
[Mem_MPh] VARCHAR (15) NULL,
[Mem_DOB] DATE NULL,
[Mem_BGr] NCHAR (10) NULL,
[Mem_WAnn] DATE NULL,
[Mem_Spouse] VARCHAR (75) NULL,
[Mem_Web] VARCHAR (300) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
我想将数据插入第一个表,然后使用相同的创建按钮将Mem_Basic
表中新插入的id作为Mem_Id
插入Mem_Details
表。
之后我想返回第一个表的Id。我使用以下c#代码
public int CreateNewMember(string Mem_NA, string Mem_Occ )
{
using (SqlConnection con=new SqlConnection(Config.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Mem_Basic(Mem_Na, Mem_Occ) output INSERTED.Id VALUES(@na, @occ)", con))
{
cmd.Parameters.AddWithValue("@na", Mem_NA);
cmd.Parameters.AddWithValue("@occ", Mem_Occ);
con.Open();
int modified = (int)cmd.ExecuteScalar();
using (SqlCommand cmd1 = new SqlCommand("INSERT INTO Mem_Details (Mem_Id, Mem_Role) VALUES (@id, @role)", con))
{
cmd.Parameters.AddWithValue("@id", modified);
cmd.Parameters.AddWithValue("@role","Member");
}
if (con.State == System.Data.ConnectionState.Open) con.Close();
return modified;
}
}
}
这只会将数据插入第一个表并返回Id。
答案 0 :(得分:2)
我会更改以下代码
using (var conn = new SqlConnection(connectionString))
using (var command = new SqlCommand("CreateNewMember", conn) {
CommandType = CommandType.StoredProcedure }) {
command.Parameters.AddWithValue("@na", Mem_NA);
command.Parameters.AddWithValue("@occ", Mem_Occ);
command.Parameters.AddWithValue("@role","Member");
conn.Open();
modified = (int)command.ExecuteScalar();
conn.Close();
}
使用存储过程
create proc CreateNewMember
(
@na nvarchar(100),
@occ nvarchar(100),
@role nvarchar(250)
)
as
begin
declare @ID int
INSERT INTO Mem_Basic(Mem_Na,Mem_Occ) output INSERTED.Id VALUES(@na,@occ)
select @ID = Scope_Identity()
INSERT INTO Mem_Details (Mem_Id,Mem_Role) VALUES (@ID,@role)
SELECT @ID
end
答案 1 :(得分:0)
试试这个。
public int CreateNewMember(string Mem_NA, string Mem_Occ )
{
using (SqlConnection con=new SqlConnection(Config.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Mem_Basic(Mem_Na,Mem_Occ) output INSERTED.Id VALUES(@na,@occ)", con))
{
cmd.Parameters.AddWithValue("@na", Mem_NA);
cmd.Parameters.AddWithValue("@occ", Mem_Occ);
con.Open();
int modified = (int)cmd.ExecuteScalar();
using (SqlCommand cmd1 = new SqlCommand("INSERT INTO Mem_Details (Mem_Id,Mem_Role) VALUES (@id,@role)", con))
{
cmd1.Parameters.AddWithValue("@id", modified);
cmd1.Parameters.AddWithValue("@role","Member");
cmd1.ExecuteNonQuery();
}
if (con.State == System.Data.ConnectionState.Open) con.Close();
return modified;
}
}
}
答案 2 :(得分:0)
您需要在第二个使用块中将cmd更改为cmd1,然后调用exert标量
public int CreateNewMember(string Mem_NA, string Mem_Occ )
{
using (SqlConnection con=new SqlConnection(Config.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Mem_Basic(Mem_Na,Mem_Occ) output INSERTED.Id VALUES(@na,@occ) SELECT SCOPE_IDENTITY()", con))
{
cmd.Parameters.AddWithValue("@na", Mem_NA);
cmd.Parameters.AddWithValue("@occ", Mem_Occ);
con.Open();
int modified = (int)cmd.ExecuteScalar();
using (SqlCommand cmd1 = new SqlCommand("INSERT INTO Mem_Details (Mem_Id,Mem_Role) VALUES (@id,@role) ", con))
{
cmd1.Parameters.AddWithValue("@id", modified);// change here
cmd1.Parameters.AddWithValue("@role","Member");// change here
cmd1.ExecuteNonQuery(); //added this
}
if (con.State == System.Data.ConnectionState.Open) con.Close();
return modified;
}
}
}