我'我有一个SQL插入问题在我的gridview
本文关键字:问题 我的 gridview 插入 SQL 有一个 | 更新日期: 2023-09-27 18:15:20
我有一个gridview,我已经设置允许插入到我的表的下一行,我有一个问题,虽然…我有2个插入语句和一个选择语句,在插入完成后更新表视图。我插入值到两个表…第一个更新一个人的信息,第二个更新旅行计划,将他们包括在旅行中。
第二个语句是我遇到问题的地方…当一个新的记录被插入到数据库中,一个新的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();
}
我个人会将其分成至少两个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()
方法-并且您将获得新插入的PersonID
作为该调用的结果。现在,您可以将数据插入TripSchedule
表中(同样,请在此调用中也使用所有参数)。
或者:将所有这些都打包到一个存储过程中,该存储过程在内部处理所有这些T-SQL功夫。
或者更好:在这些日子里,我个人会切换到像实体框架这样的ORM,使所有更容易