繁体   English   中英

SELECT IDENTITY未获取最新创建的ID

[英]SELECT IDENTITY not grabbing the most recent ID created

在另一个问题中,我问过如何配置ExecuteScalar并使用SCOPE_INDENTITY来获取我新创建的PK PersonID 将人输入到表中的第一组插入语句正在工作, PersonID在新创建的记录中位于#39。 当我在AddNewCustomer方法上设置一个断点并按代码进行操作时, newPersonID设置为“ 1”并一直停留在该位置。 有谁知道为什么我为newPersonID新建的记录未显示“ 39或40”。

protected void AddNewCustomer(object sender, EventArgs e)
{

string nFirstName = ((TextBox)GridView1.FooterRow.FindControl("txtFirstName")).Text;
string nLastName = ((TextBox)GridView1.FooterRow.FindControl("txtLastName")).Text;
string nEmergency = ((TextBox)GridView1.FooterRow.FindControl("txtEmergency")).Text;
string nCell = ((TextBox)GridView1.FooterRow.FindControl("txtCell")).Text;
string nAge = ((TextBox)GridView1.FooterRow.FindControl("txtAge")).Text;
string nActivityCard = ((TextBox)GridView1.FooterRow.FindControl("txtActivityCard")).Text;
string nInitials = ((TextBox)GridView1.FooterRow.FindControl("txtInitials")).Text;
string nBoat = ((TextBox)GridView1.FooterRow.FindControl("txtBoat")).Text;
string nGroup = ((TextBox)GridView1.FooterRow.FindControl("txtGroup")).Text;


    SqlConnection con = new SqlConnection(strConnString);
    SqlCommand cmd = new SqlCommand("INSERT INTO Person(FirstName, LastName, Emergency#, Cell#, Age, ActivityCard, CraftType, Initials, Group#) " +
    "values(@FirstName, @LastName, @Emergency, @Cell, @Age, @ActivityCard, @Boat, @Initials, @Group); " +
    "SELECT SCOPE_IDENTITY();");

    cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = nFirstName;
    cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = nLastName;
    cmd.Parameters.Add("@Emergency", SqlDbType.NChar).Value = nEmergency;
    cmd.Parameters.Add("@Cell", SqlDbType.NChar).Value = nCell;
    cmd.Parameters.Add("@Age", SqlDbType.NChar).Value = nAge;
    cmd.Parameters.Add("@ActivityCard", SqlDbType.NChar).Value = nActivityCard;
    cmd.Parameters.Add("@Initials", SqlDbType.NChar).Value = nInitials;
    cmd.Parameters.Add("@Boat", SqlDbType.VarChar).Value = nBoat;
    cmd.Parameters.Add("@Group", SqlDbType.VarChar).Value = nGroup;
    cmd.Parameters.AddWithValue("@Date", TextBox1.Text);
    cmd.Parameters.AddWithValue("@Time", ddlTripTime.SelectedItem.ToString());
    cmd.Parameters.AddWithValue("@Type", ddlTripType.SelectedItem.ToString());


    cmd.Connection = con;
    con.Open();
    int newPersonID = System.Convert.ToInt32(cmd.ExecuteScalar());
    con.Close();

    SqlCommand cmd1 = new SqlCommand();
    cmd1.CommandType = CommandType.Text;
    cmd1.CommandText ="insert into TripSchedule(TripType, PersonID, Time, Date) values (@Type, @newPerson, @Time, @Date);" +
    "SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
    "Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
    "FROM Person INNER JOIN " +
    "TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = @Date and " +
    "TripSchedule.Time = @Time and TripSchedule.TripType = @Type;";

    cmd1.Parameters.AddWithValue("@Date", TextBox1.Text);
    cmd1.Parameters.AddWithValue("@Time", ddlTripTime.SelectedItem);
    cmd1.Parameters.AddWithValue("@Type", ddlTripType.SelectedItem);
    cmd1.Parameters.AddWithValue("@newPerson", newPersonID);
    GridView1.DataSource = GetData(cmd);
    GridView1.DataBind();

}

编辑每个OP对另一个答案的评论,这是实际的错误消息:

DataBinding: 'System.Data.DataRowView' does not contain a property with the 
name 'FirstName'. 

这是我的GridView的第一个ItemTemplate。 当我在gridview的文本框中输入新数据,然后单击链接到AddNewCustomer方法的提交按钮时,出现此错误消息

您的查询包括2个选择

SqlCommand cmd = new SqlCommand("SELECT Person.PersonID, 
...
"SELECT SCOPE_IDENTITY();");

所以当你执行

int newPersonID = System.Convert.ToInt32(cmd.ExecuteScalar());

您总是得到Person.PersonID为1。

更新:

1)

您将获得DataBinding:'System.Data.DataRowView'不包含名称为'FirstName'的属性。 因为您将数据绑定到

GridView1.DataSource = GetData(cmd);
GridView1.DataBind();

因此,如果需要绑定任何数据,则删除的SELECT应该位于查询的末尾。 例:

INSERT ...;
INSERT ...;
SELECT ...

这将更具逻辑性,因为您先插入数据,然后选择一些要显示在页面上的结果。

2)您可以将2个INSERT查询“合并”为1个调用。 您只需要做

INSERT ...;
INSERT ... VALUES(..., SCOPE_IDENTITY());
SELECT ...

即:

SqlCommand cmd = new SqlCommand("INSERT INTO Person(FirstName, LastName, Emergency#, Cell#, Age, ActivityCard, CraftType, Initials, Group#) " +
    "values(@FirstName, @LastName, @Emergency, @Cell, @Age, @ActivityCard, @Boat, @Initials, @Group);"
+ "insert into TripSchedule(TripType, PersonID, Time, Date) values (@Type, SCOPE_IDENTITY(), @Time, @Date);" +
    "SELECT Person.PersonID, Person.FirstName AS FirstName, Person.LastName AS LastName, Person.Emergency# AS Emergency#, Person.Cell# AS Cell#, Person.Age AS Age, " +
    "Person.ActivityCard AS ActivityCard, Person.CraftType AS CraftType, Person.Initials AS Initials, Person.Group# AS Group# " +
    "FROM Person INNER JOIN " +
    "TripSchedule ON Person.PersonID = TripSchedule.PersonID where TripSchedule.Date = @Date and " +
    "TripSchedule.Time = @Time and TripSchedule.TripType = @Type;";

底线:

将所有sql代码移到存储过程中。

排序列表<object>通过抓取具有最近日期的重复项<div id="text_translate"><p>我有一个包含(ID,日期)的对象列表。 在某些情况下,IDS 会重复,但日期不同。 这两个都是字符串。</p><p> 我需要通过获取 ID 和最近日期对列表进行排序,同时删除较旧的日期。</p><p> 例子</p><pre>ID: 1 Date 1-1-2018 ID: 1 Date: 1-1-2019</pre><p> 我试过使用下面的代码。</p><pre> unsortedList.GroupBy(x =&gt; x.Id).Select(group =&gt; group.OrderByDescending(record =&gt; Convert.ToDateTime(record.Date)));</pre><p> 我仍然看到原始形式的 ID。</p><pre> public class Record { public string Id { get; set; } public string Date { get; set; } }</pre></div></object>

[英]Sort List<Object> with Duplicates by grabbing the one with the most recent date

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM