Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. Imports System.Data.SqlClient
  2.  
  3. Public Function conectar() As SqlConnection
  4. Try
  5. cone.Open()
  6. Return cone
  7. Catch ex As Exception
  8. MsgBox(ex.Message)
  9. Return Nothing
  10. End Try
  11. End Function
  12. Public Function desconectar() As SqlConnection
  13. Try
  14. If cone.State = ConnectionState.Open Then
  15. cone.Close()
  16. End If
  17. Return cone
  18. Catch ex As Exception
  19. Return Nothing
  20. End Try
  21. End Function
  22.  
  23. Public Class DArchivo
  24. Private conex As Miconexion
  25. Dim cmd As SqlCommand ''recibe el comando SQL SErver
  26. Dim da As New SqlDataAdapter ''Adaptaa el comando para que el visual lo entienda
  27. Dim tabla As New DataTable
  28. Public Function ReadBinaryFile(ByVal fileName As String) As Byte()
  29.  
  30. ' Generar una excepción si no existe el archivo.
  31. '
  32. If (Not IO.File.Exists(fileName)) Then
  33. Throw New IO.FileNotFoundException("No existe el archivo especificado.")
  34. End If
  35.  
  36. ' Leer el archivo especificado devolviendo una matriz de bytes con su contenido.
  37. '
  38. Return IO.File.ReadAllBytes(fileName)
  39.  
  40. End Function
  41. Public Function REGISTRARARCHIVO(ByVal nombre As String, ByVal ext As String, ByVal fila As String) As Boolean
  42. Try
  43. Dim blob As Byte() = ReadBinaryFile(fila)
  44.  
  45. conex = New Miconexion
  46. cmd = New SqlCommand("Sp_RegArchivo", conex.conectar())
  47. cmd.CommandType = CommandType.StoredProcedure
  48. cmd.Parameters.AddWithValue("@nombre", nombre)
  49. cmd.Parameters.AddWithValue("@extenssion", ext)
  50. cmd.Parameters.AddWithValue("@fila", If(blob IsNot Nothing, blob, CObj(DBNull.Value)))
  51.  
  52. If cmd.ExecuteNonQuery Then
  53. Return True
  54. Else
  55. Return False
  56. End If
  57.  
  58. Return True
  59. Catch ex As Exception
  60. MsgBox(ex.Message)
  61. Return False
  62. Finally
  63. conex.desconectar()
  64. End Try
  65. End Function
  66.  
  67. Public Function MOSTRARARCHIVO() As DataTable
  68. Try
  69. conex = New Miconexion
  70. cmd = New SqlCommand("Sp_MostrarArchivo", conex.conectar())
  71. da = New SqlDataAdapter(cmd)
  72.  
  73. tabla.Clear()
  74. da.Fill(tabla)
  75. Return tabla
  76.  
  77. Catch ex As Exception
  78. MsgBox(ex.Message)
  79. Return Nothing
  80. Finally
  81. conex.desconectar()
  82. End Try
  83. End Function
  84. Public Function descargar(ByVal id As String, ByVal ext As String, ByVal ruta As String, ByVal nombre As String) As Boolean
  85. Try
  86. conex = New Miconexion
  87. cmd = New SqlCommand("MuestraArchivo", conex.conectar())
  88. da = New SqlDataAdapter(cmd)
  89. tabla.Clear()
  90. da.Fill(tabla)
  91.  
  92. If tabla.Rows.Count = 0 Then
  93. MsgBox("No hasy DAtos")
  94. Else
  95. id = tabla.Rows(0).Item("IdArchivo")
  96. Dim res As Byte() = tabla.Rows(3).Item("Fila")
  97. File.WriteAllBytes(ruta & ext, res)
  98. End If
  99. Return True
  100. Catch ex As Exception
  101. MsgBox(ex.Message)
  102. Return False
  103. End Try
  104. End Function
  105.  
  106. Private Sub Button4_Click_1(sender As Object, e As EventArgs) Handles Button4.Click
  107.  
  108. If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
  109. txtcarpeta.Text = FolderBrowserDialog1.SelectedPath
  110.  
  111. End If
  112. End Sub
  113.  
  114. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  115. Try
  116.  
  117. fun = New DArchivo
  118. If fun.descargar(id, extension, txtcarpeta.Text, nom) Then
  119. MsgBox("Listo")
  120. Else
  121. MsgBox("No listo")
  122. End If
  123. Catch ex As Exception
  124. MsgBox(ex.Message)
  125. End Try
  126. End Sub
  127.  
  128. Imports System.Security.Permissions
  129.  
  130. Public Property ruta As String
  131.  
  132.  
  133.  
  134. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  135. Dim openfile As New OpenFileDialog()
  136. Dim nombre As String
  137. Try
  138. openfile.Filter = "Archivos de Texto (*.txt)|*.txt|Archivos de Imagen (*.jpg,*.png,*.bmp,*.gif)|*.jpg;*.png;*.bmp;*.gif|All Files (*.*)|*.*"
  139. If openfile.ShowDialog = Windows.Forms.DialogResult.OK Then
  140.  
  141. nombre = openfile.FileName
  142.  
  143. txtruta.Text = nombre
  144. End If
  145. Catch ex As Exception
  146. MsgBox("Debe elegir una imagen", MsgBoxStyle.Exclamation, "HogarSoft")
  147. End Try
  148. End Sub
  149.  
  150. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  151. Try
  152. obtenerNombreExtension()
  153. fun = New DArchivo
  154. fun.ReadBinaryFile(txtruta.Text)
  155. If fun.REGISTRARARCHIVO(nombreArchivo, extension, txtruta.Text) Then
  156. MessageBox.Show("Registro", "Registrado Correctamente", MessageBoxButtons.OK, MessageBoxIcon.Information)
  157. mostrar()
  158. Else
  159. MessageBox.Show("Registro", "No registrado", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  160. End If
  161.  
  162.  
  163. Catch ex As Exception
  164. MsgBox(ex.Message)
  165. End Try
  166.  
  167. End Sub
  168.  
  169. Private Sub Archivos_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  170. mostrar()
  171. End Sub
  172. Sub mostrar()
  173. Try
  174. fun = New DArchivo
  175. DataGridView1.DataSource = fun.MOSTRARARCHIVO
  176. Catch ex As Exception
  177. MsgBox(ex.Message)
  178. End Try
  179. End Sub
  180. Sub obtenerNombreExtension()
  181. ''largo de la cadena
  182. Dim largo As Integer
  183. largo = txtruta.Text.Length
  184. ''encontrar el punto para poder sacar la extension del archivo
  185. Dim tbus As String = "."
  186. Dim encon As Integer = txtruta.Text.IndexOf(tbus)
  187.  
  188. ''saca el tamaño de la extension del archivo esta puede variar
  189. Dim textencion As Integer
  190. textencion = largo - encon
  191. ''esto obtiene el valor de la extension del archivooo :D
  192. extension = Mid(txtruta.Text, encon + 1, textencion)
  193. ''invierte la cadenaa para trabajar mejor
  194. Dim cadinv As String = StrReverse(txtruta.Text)
  195. Dim simb As String = ""
  196. Dim indicesimb As Integer = cadinv.IndexOf(simb)
  197. Dim nom As String = Mid(cadinv, 1, indicesimb)
  198. ''vuelve a invertir la cedena arreglandola
  199. Dim nombre As String = StrReverse(nom)
  200. ''busca en que posicion esta el punto
  201. Dim punto As Integer = nombre.IndexOf(".")
  202. ''saca el tamaño de laextencion del archivo
  203. Dim tpunto As Integer = nombre.Length - punto
  204. 'extrae el nombre del archivo sin la extension
  205. nombreArchivo = Mid(nombre, 1, nombre.Length - tpunto)
  206. End Sub
  207. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  208. Try
  209.  
  210. fun = New DArchivo
  211. If fun.descargar(id, extension, txtcarpeta.Text, nom) Then
  212. MsgBox("Listo")
  213. Else
  214. MsgBox("No listo")
  215. End If
  216. Catch ex As Exception
  217. MsgBox(ex.Message)
  218. End Try
  219. End Sub
  220. Private Sub Button4_Click_1(sender As Object, e As EventArgs) Handles Button4.Click
  221.  
  222. If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
  223. txtcarpeta.Text = FolderBrowserDialog1.SelectedPath
  224.  
  225. End If
  226. End Sub
  227.  
  228. Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
  229. id = DataGridView1.SelectedCells.Item(0).Value
  230. nom = DataGridView1.SelectedCells.Item(1).Value
  231. MsgBox(id)
  232. MsgBox(nom)
  233. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement