Ver Mensaje Individual
  #5 (permalink)  
Antiguo 11/06/2009, 13:54
rmulfus
 
Fecha de Ingreso: junio-2009
Mensajes: 14
Antigüedad: 14 años, 11 meses
Puntos: 0
Respuesta: adjuntar archivos en sql server con asp

gracias x la ayuda que me has dado Lavigne pero aun no logro hacer lo que necesito esque en el caso que se muestra en el link que me diste ellos hacen el acceso a la base de datos desde el aplicativo y yo por el contrario solo llamo a un procedimiento almacenado que realiza la insercion te pondre el codigo para que talvez asi puedas comprender mejor mi problema... gracias x tu tiempo y tu paciencia

en el click del boton adjuntar hago lo siguiente:
Dim file_ext As String
Dim filename As String
Dim myfile As Byte()
If FileUpload1.HasFile <> False Then
Dim filepath As String = FileUpload1.PostedFile.FileName
Dim pat As String = "\\(?:.+)\\(.+)\.(.+)"
Dim r As Regex = New Regex(pat)
Dim m As Match = r.Match(filepath)
file_ext = m.Groups(2).Captures(0).ToString()
filename = m.Groups(1).Captures(0).ToString()
Dim file As String = filename & "." & file_ext
myfile = getfilestream(filepath)
End If

Try
SqlDataSource1.Insert()
Catch ex As Exception
ex.tostring()
End Try

donde la funcion getfilestream(filepath) es:
Public Shared Function getfilestream(ByVal filepath As String) As Byte()
Dim stream As IO.FileStream = New IO.FileStream(filepath, _ IO.FileMode.Open, IO.FileAccess.Read)
Dim reader As IO.BinaryReader = New IO.BinaryReader(stream)
Dim myfile As Byte() = reader.ReadBytes(CType(stream.Length, Integer))
reader.Close()
stream.Close()
Return myfile
End Function

los parametros se mandan al procedimiento almacenado de la siguiente manera:
Protected Sub SqlDataSource1_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEven tArgs) Handles SqlDataSource1.Inserting
e.Command.Parameters("@nombre").Value = filename
e.Command.Parameters("@extension").Value = file_ext
e.Command.Parameters("@contenido").Value = myfile
End Sub

en la parte de asp se genera el siguiente codigo:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:bpmdocstestConnectionString %>"
InsertCommand="sp_insert_consulta_ambiental" InsertCommandType="StoredProcedure">
<InsertParameters>
<asp:Parameter Name="nombre" Type="String" />
<asp:Parameter Name="extension" Type="String" />
<asp:Parameter Name="contenido" Type="Object" />
</InsertParameters>
</asp:SqlDataSource>

como podes ver el tipo de dato del contenido osea el archivo en si es "object"

y el procedimiento almacenado es asi:
PROCEDURE [dbo].[sp_insert_consulta_ambiental]
@nombre varchar(512),
@extension varchar(50),
@contenido image --sql_variant
AS
BEGIN
insert into adjunto values
(@nombre, @extension,convert(varbinary(max),@contenido))
END

como puedes ver probe poniendo un tipo de datos image pero dice que no es compatible con sql_variant no entiendo y no se que hacer no se como arreglarlo xfa dame una mano me urge esto como podras ver.... gracias de antemano