我在gridview中遇到了SQL Insert问题

时间:2014-07-27 19:44:53

标签: c# asp.net sql sql-server gridview

我有一个gridview,我已设置允许插入到我的表的下一行,但我有一个问题...我有2个插入语句和一个select语句,在插入后更新表视图完成。我将值插入两个表中...第一个更新人员信息,第二个表更新行程安排以将其包括在旅行中。

第二个语句是我遇到问题的地方......当一个新记录插入数据库时​​,会自动为一个人创建一个新的PersonID,因为我将其设置为PK和标识符Person表,但我不确定如何获取新创建的PersonID并将其插入TripSchedule表。

以下是我插入新用户的代码:

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();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "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 ('" + ddlTripType.SelectedItem + "', WHAT GOES HERE?, '" + ddlTripTime.SelectedItem + "', '" + TextBox1.Text + "');" + 
        "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 = '" + TextBox1.Text + "' and " +
        "TripSchedule.Time = '" + ddlTripTime.SelectedItem + "' and TripSchedule.TripType = '" + ddlTripType.SelectedItem + "';";

    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;

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

1 个答案:

答案 0 :(得分:2)

我个人会将此分为至少两个SqlCommand次调用 - 一次插入Person,另一次插入TripSchedule。另外:你应该全部使用参数化查询 - 不仅仅是某些地方(而不是其他地方......)。

要获取新插入的PersonID,请使用SCOPE_IDENTITY()调用 - 如下所示:

SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO dbo.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;

conn.Open();
int newPersonID = (int)cmd.ExecuteScalar();
conn.Close();

由于此SqlCommand调用只返回一行,一列(新插入的PersonID),因此您可以轻松使用.ExecuteScalar()方法 - 然后返回新插入的{{ 1}}作为此调用的结果。现在,您可以将数据插入PersonID表格中(并再次使用所有参数进行此次调用!)。

或者:将这一切都包装到一个存储过程中,在内部处理所有这些T-SQL功夫。

或者更好的是:在这些日子和时代,我会亲自切换到像实体框架这样的ORM,使这一切更容易