且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Delete和Insert命令同时更新表?

更新时间:2023-02-05 15:33:53


这样,您的代码就没有错.要了解问题出在哪里,您必须彻底调试代码.
1)尝试从HTML中提取员工ID和更新的课程ID时,请检查您是否获得了正确的值.
2)还要检查您的插入命令是否正常工作.

您的代码存在两个问题:

1)您没有在任何地方关闭连接对象.
2)您可以在交易中删除和插入.

尝试这些技巧,并让我知道问题是否已解决.

谢谢,
Mateen
Hi,
There''s no fault in your code as such. To understand where exactly the problem is you have to debug your code thoroughly.
1) Check Whether you are getting the correct value when you are trying to extract the Employee ID and Updated Course ID from HTML.
2) Also check that your insert command is working properly or not.

Two problems that is with your code is:

1) You havn''t closed the connection object anywhere.
2) You can do delete and insert within a Transaction.

Try these tips and do let me know whether problem gets resolved or not.

Thanks,
Mateen


经过多次尝试,并在***社区的一位开发人员的大力帮助下,我能够解决它.解决方案是:

After many tries and by a great help from one of the developers in *** Community, I could be able to solve it. The solution is:

//TO KEEP TRACK OF EMPLOYEE IDS FOR WHICH WE HAVE ALREADY EXECUTED 
//DELETE STATEMENT
IList<string> _deleted=new List<string>();

foreach (Control ctrl in PlaceHolder1.Controls)
{
    ...........
    .........

    if (c is CheckBox)
    {
        CheckBox checkbox = (CheckBox)c;
        if (checkbox.Checked)
        {
            string fieldvalue = checkbox.ID;
            string employeeID = fieldvalue.Split(new string[] { "," }, 
                                   StringSplitOptions.RemoveEmptyEntries)[0];
            string courseID = fieldvalue.Split(new string[] { "," }, 
                                   StringSplitOptions.RemoveEmptyEntries)[1];

            if(!_deleted.Contains(employeeID))
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                   conn.Open();
                   using (SqlCommand cmd = new SqlCommand(deleteCommand, conn))
                   {
                      cmd.Pararmeters.AddWithValue("@employeeId", employeeID);
                      cmd.ExecuteNonQuery();
                      //DON'T EXECUTE DELETE FOR ALL CHECKED ROWS
                      _deleted.Add(employeeID);
                   }
                }
            }</string></string>



如果您使用.net,则表示ini网格速度数据采用者有1个选项.使用此属性选项,u hv插入n删除表,并在smaetime删除行.
Hi,
If u r using .net means there is 1 option ini grid veiw r data adoptor. Using this properties option u hv to insert n delete the table, rows at the smaetime.