简体   繁体   English

从SQL插入查询中获取新记录主键ID以将相同值插入另一个表中?

[英]Get the new record primary key ID from SQL insert query to insert same value into another table?

I have the following C# 我有以下C#

protected void add_button_Click(object sender, EventArgs e)
{
    string teamname = team_name_tb.Text;
    string teamstatus = "Not Started";
    string teamnotes = team_notes_tb.Text;
    string sprintid = sprint_select.SelectedValue;

    string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection myConnection = new SqlConnection(ConnectionString);

    myConnection.Open();

            String query = "INSERT INTO teams (team_name, status, notes) 
                            VALUES (@team_name, @status, @notes); 
                            INSERT INTO sprints_vs_teams (sprint_id, team_id)                    
                            VALUES (@sprint_id, @team_id)";

            SqlCommand myCommand = new SqlCommand(query, myConnection);
            myCommand.Parameters.AddWithValue("@team_name", teamname);
            myCommand.Parameters.AddWithValue("@status", teamstatus);
            myCommand.Parameters.AddWithValue("@notes", teamnotes);
            myCommand.Parameters.AddWithValue("@sprint_id", sprintid);
            myCommand.Parameters.AddWithValue("@team_id", ????);

            myCommand.ExecuteNonQuery();
            myConnection.Close();
}

In the teams table the primary key is team_id I want to be able to pull the value of this and insert it into the team_id field in the lookup table I have sprints_vs_teams . teams表中,主键是team_id我希望能够获取this的值并将其插入到查找表中的team_id字段中,我有sprints_vs_teams Is there a method of doing this, if so could you possibly provide my with some guidance, please be aware I am very new to C#. 有没有这样做的方法,如果可以的话,你可以提供一些指导,请注意我对C#很新。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Amended code 修改后的代码

protected void add_button_Click(object sender, EventArgs e)
{
    String user = Session["user_id"].ToString();
    string teamname = team_name_tb.Text;
    string teamstatus = "Not Started";
    string teamnotes = team_notes_tb.Text;
    string sprintid = sprint_select.SelectedValue;

string ConnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(ConnectionString);

myConnection.Open();

String query = "Declare @team_identity int INSERT INTO teams (team_name, status, notes) VALUES (@team_name, @status, @notes); set @team_identity = scope_identity() INSERT INTO sprints_vs_teams (sprint_id, team_id) VALUES (@sprint_id, @team_identity)";

       SqlCommand myCommand = new SqlCommand(query, myConnection);
        myCommand.Parameters.AddWithValue("@team_name", teamname);
        myCommand.Parameters.AddWithValue("@status", teamstatus);
        myCommand.Parameters.AddWithValue("@notes", teamnotes);
        myCommand.Parameters.AddWithValue("@sprint_id", sprintid);

        myCommand.ExecuteNonQuery();
        myConnection.Close();
}

I amended my code with @Sick 's answer, however it didn't seem to work. 我用@Sick的答案修改了我的代码,但它似乎没有用。 If anyone could advise on where I may have went wrong it would be greatly appreciated. 如果有人可以就我可能出错的地方提出建议,我们将不胜感激。

Use SCOPE_IDENTITY 使用SCOPE_IDENTITY

Declare @team_identity int

INSERT INTO teams (team_name, status, notes) 
                            VALUES (@team_name, @status, @notes);

set @team_identity = scope_identity()

INSERT INTO sprints_vs_teams (sprint_id, team_id)                    
                            VALUES (@sprint_id, @team_identity)"

If I am not mistaken, you have to declare team_identity as a cmd.parameter. 如果我没弄错,你必须将team_identity声明为cmd.parameter。 of course there is no param passed into it c# is still expecting one 当然没有传递进入它的参数c#仍然期待一个

cmd.paramater.addwithvalue("@team_identity", paramaterDirection.out);

Or something like that 或类似的东西

after inserting a row into teams table, write the following code to get the current value of identity and the use it during insert into sprints_vs_teams table. 插入一个行后, teams表中,填写下列代码来获得认同的当前值和使用它插入到时sprints_vs_teams表。

SqlCommand cmd=new SqlCommand("select Ident_current('teams')",Cn);
Cn.Open();
int teamid=int.Parse(Cmd.ExecuteScalar().ToString());

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据插入表并从增加的主键中获取ID - Insert data into table and get ID from increased primary key 获取最近添加的记录的主键,并作为外键插入到另一个表中 - get primary key of recently added record and insert into another table as foreign key 在SQL Server中插入新行时,如何将主键值设置为表中的其他col - How can I set the primary key value to other col in table when I insert a new row in SQL Server SQL-仅当主键值不在表中时才插入表中 - SQL- Insert into table only if the primary key value isn't already in the table C# 插入与主键具有相同值的外键 - C# insert foreign key that has same value with primary key 插入到两个表中,其中一个表具有自动增量主键,并且同一键用作另一表中的外键 - Insert into two tables with one table having an auto increment primary key and that same key is used as a foreign key in another table 如何更新现有记录并在SQL表中插入新记录? 的SQL - How to Update existing record and insert new record in SQL table ? SQL 插入记录后如何从SQL Server获取Identity值 - How to get Identity value from SQL server after insert record 从 postgres 插入返回一个值并使用它在不同的表上插入新记录 - Return a value from a postgres insert and use it to insert a new record on a different table 在非标识主键中插入值 - Insert a value in a not identity primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM