Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > ผมต้องการสร้างปุ่มในตารางเพื่อลบไฟล์ใน Folder และ ชื่อในฐานข้อมูล



 

ผมต้องการสร้างปุ่มในตารางเพื่อลบไฟล์ใน Folder และ ชื่อในฐานข้อมูล

 



Topic : 122989



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์




https://www.thaicreate.com/asp.net/asp.net-upload-files-to-database.html

จากกระทู้นี้ผมได้ลองทำตาม ผมเปลี่ยนเป็น msSql

ผมอยากจะทำปุ่มสำหรับลบไฟล์และชื่อ ในตารางเลยจะได้ไหมครับ ต้องเขียน Code อย่างไรครับ



Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), VB.NET









ประวัติการแก้ไข
2016-05-18 23:59:37
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-05-18 23:59:16 By : tikus View : 1000 Reply : 8
 

 

No. 1

Guest


ใช้ sql delete command

กับ system.io.file delete method






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-05-19 11:27:43 By : ห้ามตอบเกินวันละ 2 กระทู้
 


 

No. 2



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์


ตอนนี้ผมลบชื่อไฟล์ในฐานข้อมูลได้แล้วครับ

แต่ติดที่ลบไฟล์ใน Folder

Code (VB.NET)
    Sub modDeleteCommand(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        strSQL = "DELETE FROM member WHERE MemberID = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
        objCmd = New SqlCommand(strSQL, objConn)
        objCmd.ExecuteNonQuery()

        myGridView.EditIndex = -1
        BindData()
        
        Dim FileToDelete As String
        FileToDelete = Server.MapPath("Myfiles/")
        File.Delete(FileToDelete)
    
    End Sub


ตรงชื่อไฟล์ที่จะลบ ต้องเขียน Code ยังไงครับ


ประวัติการแก้ไข
2016-05-20 05:34:14
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-05-20 01:35:56 By : tikus
 

 

No. 3



โพสกระทู้ ( 9,542 )
บทความ ( 2 )



สถานะออฟไลน์


แล้วคุณเซฟเข้าไปยังไงล่ะครับ อ้างการ path เดียวกัน
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-05-20 03:04:33 By : Chaidhanan
 


 

No. 4



โพสกระทู้ ( 281 )
บทความ ( 0 )



สถานะออฟไลน์


ต้องลบไฟล์ในโฟลเดอร์ออกให้หมดก่อนนะครับ แล้วถึงจะลบโฟลเดอร์ได้

edit:
555 ตอบแบบเบลอๆ งมโค้ดตัวเองจนตาบวม
ตอนมาตอบกระทู้สมองเลยไม่แล่น

Code (VB.NET)
dim filepath as string = "?"
If System.IO.File.Exists(filepath) then
     System.IO.File.Delete(filepath)
end if


ที่อยู่ของไฟล์ที่อัพโหลดน่าจะรู้ใช่มั้ยครับ นำมันมาใส่ตัวแปร filepath (line 1)
ตรวจสอบว่ามีไฟล์อยู่จริง (line 2)
ลบไฟล์ดังกล่าว (line 3)


ประวัติการแก้ไข
2016-05-20 09:38:13
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-05-20 03:16:59 By : deksoke
 


 

No. 5



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์


Code ทั้งหมดครับ ผมติดอยู่จุดเดียวครับ ตรงที่กำหนดชื่อไฟล์ที่จะลบ
ทำมาทั้งคืนยังงงอยู่เลยครับ

AspNetFileUploadToDatabase1.aspx
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<script runat="server">
    Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
		
        Me.pnlUpload.Visible = False
        Me.pnlSave.Visible = True

        If Me.fiUpload.HasFile = False Then
            Me.lblText.Text = "Please select upload file!"
        Else
            Me.fiUpload.SaveAs(Server.MapPath("Myfiles/" & fiUpload.FileName))
            Me.lblText.Text = "<b>" & fiUpload.FileName & "</b> Uploaded.<br>"

            '*** Save to Database ***'
            Dim objConn As SqlConnection
            Dim objCmd As SqlCommand
            Dim strConnString, strSQL As String

            strConnString = "Server=.\SQLEXPRESS;Initial Catalog=DBnew;Trusted_Connection=True;"
            objConn = New SqlConnection(strConnString)
            objConn.Open()

            strSQL = "INSERT INTO member (Name) " & _
   "VALUES ('" & fiUpload.FileName & "')"

            objCmd = New SqlCommand()
            With objCmd
                .Connection = objConn
                .CommandType = CommandType.Text
                .CommandText = strSQL
            End With
            objCmd.ExecuteNonQuery()

            objCmd = Nothing
            objConn.Close()
            objConn = Nothing

            Me.hplLink1.NavigateUrl = "AspNetFileUploadToDatabase1.aspx"
            Me.hplLink2.NavigateUrl = "GridViewRowCommand.aspx"
        End If
    End Sub
</script>
<html>
<head>
<title>btnUpload</title>
</head>
<body>
    <form id="form1" runat="server">
		<asp:Panel id="pnlUpload" runat="server">
			<asp:FileUpload id="fiUpload" runat="server"></asp:FileUpload>
			<asp:Button ID="btnUpload" onclick="btnUpload_Click" runat="server" Text="Upload" /></asp:Button>
		</asp:Panel>
		<asp:Panel id="pnlSave" Visible="false" runat="server">
			<asp:Label id="lblText" runat="server"></asp:Label><br />
			<asp:HyperLink id="hplLink1" runat="server">Upload again</asp:HyperLink>
			,
			<asp:HyperLink id="hplLink2" runat="server">View files</asp:HyperLink>
		</asp:Panel>
    </form>
</body>
</html>


GridViewRowCommand.aspx
<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.IO" %>
<script runat="server">
    Dim objConn As SqlConnection
    Dim objCmd As SqlCommand
	Dim strSQL As String

	Sub Page_Load(sender As Object, e As EventArgs)
		   Dim strConnString As String
		   strConnString = "Server=.\SQLEXPRESS;Initial Catalog=DBnew;Trusted_Connection=True;"
        objConn = New SqlConnection(strConnString)
		   objConn.Open()

		   IF Not Page.IsPostBack() Then	
			BindData()
		   End IF
	End Sub

	Sub BindData()	   
        strSQL = "SELECT * FROM member"

        Dim dtReader As SqlDataReader
        objCmd = New SqlCommand(strSQL, objConn)
	   dtReader = objCmd.ExecuteReader()

	   '*** BindData to GridView ***'
	   myGridView.DataSource = dtReader
	   myGridView.DataBind()

	   dtReader.Close()
	   dtReader = Nothing

	End Sub

	Sub Page_UnLoad()
	   objConn.Close()
	   objConn = Nothing
	End Sub

    Sub modEditCommand(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        myGridView.EditIndex = e.NewEditIndex
        myGridView.ShowFooter = False
        BindData()
    End Sub

    Sub modCancelCommand(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
        myGridView.EditIndex = -1
        myGridView.ShowFooter = True
        BindData()
    End Sub

    Sub modDeleteCommand(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        strSQL = "DELETE FROM member WHERE MemberID = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
        objCmd = New SqlCommand(strSQL, objConn)
        objCmd.ExecuteNonQuery()

        myGridView.EditIndex = -1
        BindData()
        
        Dim FileToDelete As String
        FileToDelete = Server.MapPath("Myfiles/")
            File.Delete(FileToDelete)
    
        
    End Sub

    Sub myGridView_RowCommand(ByVal source As Object, ByVal e As GridViewCommandEventArgs)
        If e.CommandName = "Add" Then
            '*** MemberID ***'
            Dim txtMemberID As TextBox = CType(myGridView.FooterRow.FindControl("txtAddMemberID"), TextBox)
            '*** Username ***'
            Dim txtUsername As TextBox = CType(myGridView.FooterRow.FindControl("txtAddUsername"), TextBox)
            '*** Password ***'
            Dim txtPassword As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPassword"), TextBox)
            '*** Name ***'
            Dim txtName As TextBox = CType(myGridView.FooterRow.FindControl("txtAddName"), TextBox)
            '*** Email ***'
            Dim txtEmail As TextBox = CType(myGridView.FooterRow.FindControl("txtAddEmail"), TextBox)
            '*** Country ***'
            Dim txtCountry As TextBox = CType(myGridView.FooterRow.FindControl("txtCountry"), TextBox)

            strSQL = "INSERT INTO customer (MemberID,Username,Password,Name,Email,Country) " & _
            " VALUES ('" & txtMemberID.Text & "','" & txtUsername.Text & "','" & txtPassword.Text & "' " & _
            " ,'" & txtName.Text & "','" & txtEmail.Text & "','" & txtCountry.Text & "') "
            objCmd = New SqlCommand(strSQL, objConn)
            objCmd.ExecuteNonQuery()

            BindData()
        End If
    End Sub


    Sub modUpdateCommand(ByVal s As Object, ByVal e As GridViewUpdateEventArgs)

        '*** MemberID ***'
        Dim txtMemberID As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditMemberID"), TextBox)
        '*** Username ***'
        Dim txtUsername As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditUsername"), TextBox)
        '*** Password ***'
        Dim txtPassword As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPassword"), TextBox)
        '*** Name ***'
        Dim txtName As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditName"), TextBox)
        '*** Email ***'
        Dim txtEmail As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditEmail"), TextBox)
        '*** Country ***'
        Dim txtCountry As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditCountry"), TextBox)

        strSQL = "UPDATE member SET MemberID = '" & txtMemberID.Text & "' " & _
        " ,Username = '" & txtUsername.Text & "' " & _
        " ,Password = '" & txtPassword.Text & "' " & _
        " ,Name = '" & txtName.Text & "' " & _
        " ,Email = '" & txtEmail.Text & "' " & _
        " ,Country = '" & txtCountry.Text & "' " & _
        " WHERE MemberID = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
        objCmd = New SqlCommand(strSQL, objConn)
        objCmd.ExecuteNonQuery()

        myGridView.EditIndex = -1
        myGridView.ShowFooter = True
        BindData()
    End Sub

</script>
<html>
<heaThaiCreate.Com ASP.NET - GridView</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView id="myGridView" runat="server" AutoGenerateColumns="False " 
	ShowFooter="True" 
	DataKeyNames="MemberID"
	OnRowEditing="modEditCommand"
	OnRowCancelingEdit="modCancelCommand"
	OnRowDeleting="modDeleteCommand"
	OnRowUpdating="modUpdateCommand"
	OnRowCommand="myGridView_RowCommand">

	<Columns>

	<asp:TemplateField HeaderText="CustomerID">
		<ItemTemplate>
			<asp:Label id="lblMemberID" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MemberID") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditMemberID" size="5" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MemberID") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddMemberID" size="5" runat="server"></asp:TextBox>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:TemplateField HeaderText="Username">
		<ItemTemplate>
			<asp:Label id="lblUsername" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Username") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditUsername" size="20" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Username") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddUsername" size="20" runat="server"></asp:TextBox>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:TemplateField HeaderText="Password">
		<ItemTemplate>
			<asp:Label id="lblPassword" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Password") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditPassword" size="20" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Password") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddPassword" size="20" runat="server"></asp:TextBox>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:TemplateField HeaderText="Name">
		<ItemTemplate>
			<asp:Label id="lblName" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditName" size="20" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddName" size="20" runat="server"></asp:TextBox>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:TemplateField HeaderText="Email">
		<ItemTemplate>
			<asp:Label id="lblEmail" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditEmail" size="30" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddEmail" size="30" runat="server"></asp:TextBox>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:TemplateField HeaderText="Country">
		<ItemTemplate>
			<asp:Label id="lblCountry" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Country") %>'></asp:Label>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:TextBox id="txtEditCountry" size="30" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Country") %>'></asp:TextBox>
		</EditItemTemplate>
		<FooterTemplate>
			<asp:TextBox id="txtAddCountry" size="30" runat="server"></asp:TextBox>
			<asp:Button id="btnAdd" runat="server" Text="Add" CommandName="Add"></asp:Button>
		</FooterTemplate>
	</asp:TemplateField>

	<asp:CommandField ShowEditButton="True" CancelText="Cancel" DeleteText="Delete" EditText="Edit" UpdateText="Update" HeaderText="Modify"  />
	<asp:CommandField ShowDeleteButton="True" HeaderText="Delete" />
    	
	</Columns>
</asp:GridView>
</form>
</body>
</html>

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-05-20 05:33:28 By : tikus
 


 

No. 6



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์


ตอนนี้ผมทำได้แล้วครับ

อยากถามว่าเขียน Code แบบนี้ถูกไหมครับ หรือว่ามีวิธีการเขียนที่ดีกว่านี้ แนะนำหน่อยครับ

Code (VB.NET)
Sub modDeleteCommand(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
       
        strSQL = "Select Name From member WHERE MemberId = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
        objCmd = New SqlCommand(strSQL, objConn)
        objCmd.ExecuteNonQuery()
                                       
        Dim dtAdapter As New SqlDataAdapter(strSQL, objConn)
        Dim dt As New DataTable
        dtAdapter.Fill(dt)
    
        If dt.Rows.Count > 0 Then
            
            strSQL = "Delete From member WHERE MemberId = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
            objCmd = New SqlCommand(strSQL, objConn)
            objCmd.ExecuteNonQuery()
            
            Dim FileToDelete As String
            FileToDelete = Server.MapPath("Myfiles/" & dt.Rows(0)("Name"))
            File.Delete(FileToDelete)
                    
        End If
        myGridView.EditIndex = -1
        BindData()
        
                
    End Sub

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-05-20 22:16:51 By : tikus
 


 

No. 7



โพสกระทู้ ( 9,542 )
บทความ ( 2 )



สถานะออฟไลน์


เอาแค่ทำงานได้ไปก่อนเถอะครับ สำหรับดีกว่า ไว้รอประสบการณ์สอนคุณดีกว่าครับ
บอกไปก็เหมือนสอนคนพูดท่องจำ ไม่ได้ประโยชน์ ครั้งจะมาอธิบาย มันก็มากโขอยู่

เอาแค่ศึกษาเรื่องไวยกรณ์ของแต่ละภาษาให้ดีให้แม่น มันจะช่วยให้คุณเขียนได้ดีขึ้นแน่นอนครับ
ถึงแม้คุณจะไม่รู้จักฟังก์ชั่นที่คนอื่นเขียนไว้ให้ คุณก็ยังจะเขียนขึ้นมาใช้งานได้เอง จากไวยกรณ์พื้นฐานนั้นแหล่ะครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-05-21 03:48:41 By : Chaidhanan
 


 

No. 8



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-05-23 09:56:49 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ผมต้องการสร้างปุ่มในตารางเพื่อลบไฟล์ใน Folder และ ชื่อในฐานข้อมูล
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่