0

我正在使用 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)
);

我想将数据插入到第一个表中,然后使用相同的创建按钮将新插入的 id 从Mem_Basic表中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。

4

3 回答 3

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
于 2013-08-30T05:59:03.033 回答
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;
                }
            }
        }
于 2013-08-30T05:38:06.680 回答
0

您需要在第二个 using 块中将 cmd 更改为 cmd1 ,然后调用 exeute 标量

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;
            }
        }
    }
于 2013-08-30T05:44:01.877 回答