Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
mbm30075
Starting Member
4 Posts |
Posted - 2008-11-17 : 17:56:42
|
OK, so I've got a Visual Basic program that is storing BLOB data in a varbinary(max) column in SQL Server 2005. I've had a lot of trouble with this lately, and it seems like the problems didn't exist before (even with the SAME code).What I am trying to do is:1. Insert a partial row, leaving the varbinary(max) column blank (or empty with "0x0")2. Update the row, using [DOCUMENT ID] (which is NOT a hard-coded PK, but is one anyway, because of my code) to specify the row and I am trying to use UPDATE .WRITE to update the record.I keep getting Timeout errors. I'm using UPDATE .WRITE because some of my files (PDF files) get up to 100 MB+ in size, and I obviously can't stick them in the table in 1 pass. I am avoiding using OPENROWSET(BULK because I keep getting Access errors and can't figure out how to get that particular solution to work from my workstation, against a server, bulk importing files from the network.Here is the code I am TRYING to use: Private Sub File2SqlBlob(ByVal SourceFilePath As String, ByVal DocumentID As Integer) InsertRecordWithoutBinaryData(SourceFilePath, DocumentID) InsertBinaryData(SourceFilePath, DocumentID) End Sub Private Sub InsertRecordWithoutBinaryData(ByVal SourceFilePath As String, ByVal DocumentID As Integer) Try Dim strFileExt As String = New IO.FileInfo(SourceFilePath).Extension.Replace(".", "") Dim strAddRecordWithNoVarbinary As String = "INSERT INTO PDFTABLE([DOCUMENT ID], FILECODE, FILEEXT) VALUES(@DOCUMENTID, 0x0, @FILEEXT);" Dim cmdAddRecordWithNoVarBinary As New SqlClient.SqlCommand(strAddRecordWithNoVarbinary, cn) cmdAddRecordWithNoVarBinary.Parameters.AddWithValue("@DOCUMENTID", DocumentID) cmdAddRecordWithNoVarBinary.Parameters.AddWithValue("@FILEEXT", strFileExt) cn.Open() cmdAddRecordWithNoVarBinary.ExecuteNonQuery() cn.Close() Catch SQLEx As SqlClient.SqlException MsgBox("There has been an error importing " & SourceFilePath & " as Document ID: " & DocumentID & ". Please try to add this file again later.", MsgBoxStyle.OkOnly, "File Not Added to SQL") Catch ex As Exception MsgBox(ex.Message) Finally If cn.State = ConnectionState.Open Then cn.Close() End If End Try End Sub Private Sub InsertBinaryData(ByVal SourceFilePath As String, ByVal DocumentID As Integer) Try Dim bufferLen As Long = 8040 Dim strAppendRecordWithVarbinary As String = _ "UPDATE PDFTABLE SET FILECODE .WRITE(@BYTES, @OFFSET, @LENGTH) WHERE [DOCUMENT ID] = @DOCUMENTID" Dim cmdAppendRecordWithVarbinary As New _ SqlClient.SqlCommand(strAppendRecordWithVarbinary, cn) Dim filecodeParm As SqlClient.SqlParameter = _ cmdAppendRecordWithVarbinary.Parameters.Add("@BYTES", SqlDbType.VarBinary, bufferLen) Dim offsetParm As SqlClient.SqlParameter = cmdAppendRecordWithVarbinary.Parameters.AddWithValue("@OFFSET", 0) cmdAppendRecordWithVarbinary.Parameters.AddWithValue("@LENGTH", bufferLen) cmdAppendRecordWithVarbinary.Parameters.AddWithValue("@DOCUMENTID", DocumentID) Dim firstTimeThrough As Boolean = True Dim fs As IO.FileStream = New IO.FileStream(SourceFilePath, IO.FileMode.Open, IO.FileAccess.Read) Dim br As IO.BinaryReader = New IO.BinaryReader(fs) Dim buffer() As Byte = br.ReadBytes(bufferLen) Dim offset_ctr As Integer = 0 cn.Open() Do While buffer.Length > 0 If firstTimeThrough = False Then offsetParm.Value = DBNull.Value cmdAppendRecordWithVarbinary.CommandTimeout = 600 filecodeParm.Value = buffer cmdAppendRecordWithVarbinary.ExecuteNonQuery() buffer = br.ReadBytes(bufferLen) firstTimeThrough = False Loop cn.Close() Catch SQLEx As SqlClient.SqlException MsgBox("There has been an error importing " & SourceFilePath & " as Document ID: " & DocumentID & ". Please try to add this file again later.", MsgBoxStyle.OkOnly, "File Not Added to SQL") Catch ex As Exception MsgBox(ex.Message) Finally If cn.State = ConnectionState.Open Then cn.Close() End If End Try End Sub The cmdAppendRecordWithVarbinary.ExecuteNonQuery() is where I keep getting the timeout errors. What's weird, though, is that when I upped the CommandTimeout to 600 (from the default of 30), it started working, and doing so very, VERY quickly (like sub 0.1 second cycles). Then, it stopped again. This is making me insane. For a while now, I've just ignored the problem, going out of my way to figure out how to BULK import bigger files through SSMS. This annoys me, however, and I want a solution that actually works. I just can't seem to find it. What am I missing? It seems like the DB has the row locked after the INSERT, or something, but I'm just not sure, and I'm neither a VB expert nor a SQL expert.I am a self-taught coder, so if it's something basic or obvious, please be gentle. I welcome any help I can get!!! |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-11-27 : 03:20:53
|
If you query the sys.dm_exec_request view in SQL, what's the wait type for your query? What's the wait resource?--Gail ShawSQL Server MVP |
 |
|
mbm30075
Starting Member
4 Posts |
Posted - 2008-11-27 : 21:25:12
|
How would I give you the answer to that question? What code/query do I need to run and WHEN? |
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-11-28 : 02:57:50
|
Have you got any SQL people or a DBA there?--Gail ShawSQL Server MVP |
 |
|
mbm30075
Starting Member
4 Posts |
Posted - 2008-12-01 : 08:04:47
|
Unfortunately, you're talking to him :). I guess we don't have anyone on-site that is certified in SQL, but I'm familiar with a good bit of SQL (or so I thought). I'm not familiar with querying views, and it would seem like such a query would have to take place WHILE the query is running. Is that correct, or can you view results after the fact, too? Do I run this from SSMS or from my calling code? I'm really not dense, and with the right instructions, I think I can figure this out.Thanks. |
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-12-01 : 08:35:46
|
From management studio, while the problematic query from the app is running.You query the view just like a table. No differences there.--Gail ShawSQL Server MVP |
 |
|
mbm30075
Starting Member
4 Posts |
Posted - 2008-12-01 : 09:15:04
|
OK, that's about what I thought. So, I'll run the following query WHILE I'm getting a problem from the other app?SELECT * FROM SYS.DM_EXEC_REQUESTIs that right? |
 |
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-12-01 : 09:32:43
|
sys.dm_exec_requests (it's plural) Sorry, my mistake earlier.That will list all of the currently running requests on the server. Perhaps it's better to run this oen, you'll see more details and less 'fluff'SELECT er.session_id, wait_type, wait_time, wait_resource, host_name, program_name, original_login_name, er.reads, er.writes, er.cpu_time, blocking_session_id, st.text FROM sys.dm_exec_requests er INNER JOIN sys.dm_exec_sessions es on er.session_id = es.session_id CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) stThere will be multiple rows. One will be for the query itself, one should be the VB app, there may be others. See if you can match the st.text column (contains the SQL statement), hostname or program name to the VB app.See if the blocking_session_id is 0 or not, see if there's a wait_type.--Gail ShawSQL Server MVP |
 |
|
|
|
|
|
|