遍历表中的数据库行 [英] loop throught database row in a table

查看:60
本文介绍了遍历表中的数据库行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我有一个问题,我想从一个名为EmpData的表中删除原始数据,其中Empusername等于另一个表的值.我的问题是,存在多个行,我如何循环遍历这些行以测试它们并删除其中的数据?

Hello
I have a Problem i want to delete raws from a table named EmpData where Empusername is equale to a value of another table my problem was that there is more than one row how can i loop through rows to test them and delete the data in them ?

 Protected Sub DELETE(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView3.SelectedIndexChanged
        empUsername.Text = GridView3.SelectedRow.Cells(5).Text.ToString
      
       

        Dim path3 As String = Server.MapPath("~/Uploads/audio/")
        Dim path As String = Server.MapPath("~/Uploads/videos/")
        Dim path2 As String = Server.MapPath("~/Uploads/pictures/")
        Dim conn As String = ConfigurationManager.ConnectionStrings("projectConnectionString").ConnectionString
        Dim connection As New SqlConnection(conn)
        connection.Open()
        Dim cmd As New SqlCommand("SELECT DateOfUpload, DescOfWork,WorkDone,ComOfEmp,Picture,EmpUsername,Video,Audio FROM EmpData WHERE EmpUsername = ''" & empUsername.Text & "''", connection)
        cmd.ExecuteNonQuery()
        Dim lrd As SqlDataReader = cmd.ExecuteReader




        While lrd.HasRows()
            lrd.Read()
            deleteuser.Text = lrd.Item("EmpUsername").ToString
            movie1.Text = lrd.Item("Video").ToString
            audio1.Text = lrd.Item("Audio").ToString
            image1.Text = lrd.Item("Picture").ToString
            If Not image1.Text = "" Then
                System.IO.File.Delete(path2 + image1.Text)
            End If
            If Not audio1.Text = "" Then
                System.IO.File.Delete(path3 + audio1.Text)
            End If
            If Not movie1.Text = "" Then
                System.IO.File.Delete(path + movie1.Text)
            End If
            lrd.Close()
            Dim delete As New SqlCommand("DELETE FROM EmpData WHERE EmpUsername = ''" & deleteuser.Text & "''", connection)
            delete.ExecuteNonQuery()
            lrd = cmd.ExecuteReader
        End While
        lrd.Close()
       
        connection.Close()

        Response.Redirect("administrator.aspx?data=" + TextBox1.Text, False)
end sub



它总是删除该行,但不会删除保存的url链接的数据,而是仅删除第一行的数据,如果有人可以在这里帮助我,请



it always delete the row but the data linked by the url saved are not deleted only the data of the first row are deleted please if anyone can help me here

推荐答案

C#,我会:
获取主键列表(id)
in C#, i would:
get List of Primary Keys (id)
List<int> idList = new List<int>();
string sqlString = " select TableID from Table where Item = '" + item+ "'";

//while has rows add id to list...

//foreach id in idList do...
string sqlString = "delete from Table where TableID = '" + id.ToString() + "'";
</int></int>


您已经写道:
我想从名为EmpData的表中删除行,其中Empusername 等于另一个表的值"

请分析您的代码. 可能是您将从同一张表中删除数据.您的查询如下:
You have wrote:
"i want to delete rows from a table named EmpData where Empusername is equal to a value of another table"

Please, analyze your code. Probably, you are going to delete data from the same table. Your query looks like:
DELETE
FROM EmpData 
WHERE EmpUsername IN (SELECT EmpUsername FROM EmpData WHERE EmpUsername = 'Username')



您需要的是:



All that you need is:

DELETE
FROM EmpData 
WHERE EmpUsername = 'Username'


这篇关于遍历表中的数据库行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆