Question : Uploading a file into sql server database using Image datatype

Hi,

I have a vb.net application (win 32) and am simply trying to insert a record into the database.  The below code works but the problem is that the code is selecting all of the records from the database and updating all of the records from the database.  What is a more efficient way of doing this so that I am only inserting the record I need to insert?  Like I said, the code works and inserts the record but doing it this way takes a major performance hit. See code below!  Thanks in advance for any and all help!

            Dim ToPath As String
            ToPath = Me.txtFilePath.Text
            ToPath = ToPath.Substring(ToPath.LastIndexOf("\") + 1)

            Dim FileName As String
            FileName = ToPath
            Dim FileExt As String
            FileExt = ToPath.Substring(ToPath.LastIndexOf(".") + 1)

            da = New SqlClient.SqlDataAdapter("SELECT * FROM tblFileUploads", SqlConn)
            Dim MyCB As SqlClient.SqlCommandBuilder = New SqlClient.SqlCommandBuilder(da)

            da.MissingSchemaAction = MissingSchemaAction.AddWithKey

            Dim fs As New FileStream(Me.txtFilePath.Text, FileMode.OpenOrCreate, FileAccess.Read)
            Dim MyData(fs.Length) As Byte
            fs.Read(MyData, 0, fs.Length)
            fs.Close()

            SqlConn.Open()



            da.Fill(ds, "MyImages")
            Dim myRow As DataRow
            myRow = ds.Tables("MyImages").NewRow

            myRow("RevisionID") = Me.txtRevisionID.Text
            myRow("FilePath") = ToPath
            myRow("FileDescription") = u.FTM(Me.txtFileDescription.Text)
            myRow("ImgField") = MyData
            myRow("FileName") = FileName
            myRow("FileExt") = FileExt

            ds.Tables("MyImages").Rows.Add(myRow)
            da.Update(ds, "MyImages")

            fs = Nothing
            MyCB = Nothing
            ds.Tables("MyImages").Clear()

            SqlConn.Close()

Answer : Uploading a file into sql server database using Image datatype

Why don't you simply execute a single record INSERT INTO sql statement?  Is the field RevisionID the primary Key on the table?

If you must use the appraoch that you have here, then make this simple change:

da = New SqlClient.SqlDataAdapter("SELECT * FROM tblFileUploads where RevisionID = " & Me.txtRevisionID.Text , SqlConn)

which will return an EMPTY dataadapter (and will do so VERY QUICKLY), and you can then add the new record (also very quickly).

AW

 

Random Solutions  
 
programming4us programming4us