none
Help needed with adding an image to a database RRS feed

  • Question

  • Hi, ive been trying to add images into a sql database in visual studio 2005. I have searched to find a solution and found this visual basic example http://msdn2.microsoft.com/en-us/library/4f5s1we0(vs.80).aspx

    I have been trying to alter this example so that it fits with my information, but im coming across problems.

    I have modified the code as follows

    Code Snippet

    Public Shared Sub AddImage( _

    ByVal ImageNum As Integer, _

    ByVal photoFilePath As String, _

    ByVal connectionString As String)

    Dim photo() As Byte = GetPhoto(photoFilePath)

    Using connection As SqlConnection = New SqlConnection(connectionString)

    Dim command As SqlCommand = New SqlCommand( _

    "INSERT INTO Image (ImageNum, Photo) " & _

    "Values(@ImageNum, @Photo)", connection)

    command.Parameters.Add("@ImageNum", _

    SqlDbType.Int).Value = ImageNum

    command.Parameters.Add("@Photo", _

    SqlDbType.Image, photo.Length).Value = photo

    connection.Open()

    command.ExecuteNonQuery()

    End Using

    End Sub

    Public Shared Function GetPhoto(ByVal filePath As String) As Byte()

    Dim stream As FileStream = New FileStream( _

    filePath, FileMode.Open, FileAccess.Read)

    Dim reader As BinaryReader = New BinaryReader(stream)

    Dim photo() As Byte = reader.ReadBytes(stream.Length)

    reader.Close()

    stream.Close()

    Return photo

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    AddImage(TextBox1.Text, TextBox2.Text, "OMSDBConnectionString")

    End Sub

     

    But i have but i have been getting errors with the conneciton string, im not sure where ive gone wrong in modifying this. The error i get is "Format of the initialization string does not conform to specification starting at index 0."

    I want to be able to add images to the database so i can use them in the report viewer. Can anyone help me with getting this to work please?

    Thursday, September 20, 2007 1:49 PM

All replies

  • Is OMSDBConnectionString a variable that by chance that got turned into a string literal?

     

     

    Thursday, September 20, 2007 3:06 PM
  • Sorry im a bit new to this, the OMSDBConnectionString is the name i gave the connection at the start, its whats seen if you go into the database configuration wizard when selecting a connection for the database. Is this name not what i need to be using?

    Thursday, September 20, 2007 3:34 PM
  • connection string should be of following format

     

    If SQL Authentication is Enabled in SQL Server

    "Server = <DB Server Name>; Initial Catalog = <Database Name>; uid = <SQL Server User Id>; pwd = <Password>"

     

    If Windows Authentication is Enabled in SQL Server

    "Server = <DB Server Name>; Initial Catalog = <Database Name>; Integrated Security = SSPI"

    Friday, September 21, 2007 5:35 AM
  • I tried adding it in, but i must have put it in the wrong place as i replaced "OMSDBConnectionString" the string in the format you said, i inserted the server name and database names with the ones i used. It didnt seem to work though, as i got blue underlines for errors under Server, the DB Server Name, also the ; character. Am i supposed to declare the connection string first? if so where in the code above should i declare it?

     

    Monday, September 24, 2007 3:28 PM