
ExecuteNonQuery requiere una connection abierta y disponible. Elestado actualdelaconexionescerrada
Publicado por Alan (3 intervenciones) el 22/10/2015 22:14:27
Hola me aparece el mensaje "ExecuteNonQuery requiere una connection abierta y disponible. El estado actual de la conexion es cerrada" cuando le doy clic al guardar una imagen en mi proyecto, mi codigo es el siguiente.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Imports System.Data.OleDb
Imports System.IO
Public Class Fotos
Private conex As New OleDbConnection
Private Sub conexion()
conex.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|imagen.mdb"
conex.Open()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conexion()
picFoto.SizeMode = PictureBoxSizeMode.CenterImage
End Sub
Private Sub btnExa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExa.Click
Dim ofd As New OpenFileDialog
ofd.Filter = "Imagenes JPG |*.jpg"
ofd.RestoreDirectory = True
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
picFoto.Image = Image.FromFile(ofd.FileName)
End If
End Sub
Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
Try
Dim ms As New MemoryStream
picFoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim pic As Byte() = ms.GetBuffer
If txtNombre.Text <> "" And pic.Length > 0 Then
Dim cmd As New OleDbCommand("insert into imagenes values(@nom,@pic)", conex)
cmd.Parameters.Add(New OleDbParameter("@nom", txtNombre.Text))
cmd.Parameters.Add(New OleDbParameter("@pic", pic))
cmd.ExecuteNonQuery()
MsgBox("Los registros se han almacenado correctamente", MsgBoxStyle.Information)
Else
MsgBox("Debe agregar todos los datos")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
Valora esta pregunta


0