I'm trying to use the
OpenSqlFilestream
Instruction but my code did not recognise it
What dll i have to load in order to use it?
Dear marc_s look at my code
Public Sub WriteFileStream(ByVal imSource As Byte(), ByVal imSize As Integer, ByVal imTy As Type, ByVal sender As Object, ByVal e As EventArgs)
Dim subProvider As String = Nothing
Dim subDataSource As Object = Nothing
Dim subUid As String = Nothing
Dim subPwd As String = Nothing
Dim subDataBase As String = Nothing
Dim subPSI As Boolean = Nothing
Dim ParamXML() As String = Nothing
Dim TypeOfServer As String = "Remote"
Dim imParam(3) As String
Dim imTable(1) As String
Dim RemoteSQLcmd As SqlCommand = New SqlCommand
Dim tokenReader As SqlDataReader
'-------------------------------------------------'
Dim AbsRecord As Int64 = 0
Dim VarString(10) As String
Dim VarInt(10) As Integer
'-------------------------------------------------'
ParamXML = Split(loadXmlFile(TypeOfServer, sender, e), "|")
subUid = ParamXML(3)
subProvider = ParamXML(0)
subDataSource = ParamXML(1)
subDataBase = ParamXML(2)
subPwd = ParamXML(4)
subPSI = ParamXML(5)
Dim SchemaID As String = Convert.ToInt16(ParamXML(8))
Dim SchemaName As String = Nothing
If SchemaID = 1 Then
SchemaName = subUid
ElseIf SchemaID = 2 Then
SchemaName = "dbo"
ElseIf SchemaID = 0 Then
SchemaName = "dbo"
End If
'-------------------------------------------------'
imTable(0) = "tPDetails"
imTable(1) = "tPImages"
Try
imParam(0) = Me.TextBox1.Text.Trim.ToString 'Name'
imParam(1) = Me.TextBox2.Text.Trim.ToString 'Code'
imParam(2) = Me.TextBox3.Text.Trim.ToString 'Price'
imParam(3) = Me.TextBox4.Text.Trim.ToString 'Comments'
'========================================================'
If RemoteSQLConn.State = ConnectionState.Open Then RemoteSQLConn.Close()
SQL_Connection(TypeOfServer, TypeOfServer & "Conn.xml", sender, e)
RemoteSQLConn.open()
'----------------------'
Dim imHolder As Image = Image.FromStream(imStream)
Dim imHeight As Integer = imHolder.Height
Dim imWidth As Integer = imHolder.Width
Dim imLength As Integer = imHolder.PropertyItems.Length
Dim imType As Type = imTy
'----------------------'
Dim FirstColumnNames As String = _
imTable(0) & "_Code, " & _
imTable(0) & "_Price, " & _
imTable(0) & "_Title, " & _
imTable(0) & "_Type, " & _
imTable(0) & "_Height, " & _
imTable(0) & "_Width, " & _
imTable(0) & "_Stock, " & _
imTable(0) & "_Comments "
Dim FirstFieldsValues As String = "'" & imParam(1) & "', '" & _
imParam(2) & "', '" & _
imParam(0) & "', '" & _
imType.ToString & "', '" & _
imHeight & "', '" & _
imWidth & "', '" & _
"0', '" & _
imParam(3) & "' "
'--------------------------------------------'
RemoteSQLcmd = New SqlCommand("INSERT INTO " & _
SchemaName & "." & imTable(0) & " (" & FirstColumnNames & ") VALUES (" & FirstFieldsValues & ") ", RemoteSQLConn)
RemoteSQLcmd.ExecuteNonQuery()
'--------------------------------------------------'
RemoteSQLcmd = New SqlCommand("SELECT * FROM " & SchemaName & "." & imTable(0) & _
" WHERE " & imTable(0) & "_Code = " & "'" & imParam(1) & "'", RemoteSQLConn)
AbsRecord = RemoteSQLcmd.ExecuteScalar
'--------------------------------------------------'
RemoteSQLcmd = New SqlCommand("INSERT INTO " & SchemaName & "." & imTable(1) & " VALUES (newid(), " & AbsRecord & ", CAST('' as varbinary(max)))", RemoteSQLConn)
RemoteSQLcmd.ExecuteNonQuery()
'--------------------------------------------------'
RemoteSQLcmd = New SqlCommand("SELECT " & imTable(1) & "_Image.PathName() FROM " & _
SchemaName & "." & imTable(1) & " WHERE " & imTable(1) & "_" & imTable(0) & "_ID = " & AbsRecord, RemoteSQLConn)
Dim filePathName As String = Nothing
Dim pathObj As Object = RemoteSQLcmd.ExecuteScalar()
'-------------------------------------------------- Path Name '
If Not pathObj.Equals(DBNull.Value) Then
filePathName = DirectCast(pathObj, String)
Else
Throw New System.Exception("Image.PathName() failed to read the path name for the Image column.")
End If
'-------------------------- GET_FILESTREAM_TRANSACTION_CONTEXT()'
Dim RemoteSQLtx As SqlTransaction = RemoteSQLConn.BeginTransaction("MainTranaction")
RemoteSQLcmd.Transaction = RemoteSQLtx
RemoteSQLcmd = New SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", RemoteSQLConn, RemoteSQLtx)
Dim tokenObject As Object = RemoteSQLcmd.ExecuteScalar()
'-------------------------------------- File Token '
tokenReader = RemoteSQLcmd.ExecuteReader(CommandBehavior.SingleRow)
tokenReader.Read()
Dim txContext As SqlBinary = DirectCast(tokenObject, Byte())
tokenReader.Close()
'-----------------------------------------------'
Try
'-------------------- Closing all connections'
If RemoteSQLConn.State = ConnectionState.Open Then RemoteSQLConn.Close()
If RemoteSQLcmd.Connection.State = ConnectionState.Open Then RemoteSQLcmd.Connection.Close()
If RemoteConnInfo.State = ConnectionState.Open Then RemoteConnInfo.Close()
RemoteSQLtx.Dispose() '.Connection.State = ConnectionState.Open Then RemoteSQLtx.Connection.Close()
'-------------------- Open connections for Indegrated Security''
ChangeLoginPerson("PRINCIDEVEL\Administrator")
RemoteConnInfo.ConnectionString = "Provider=" & subProvider & "; Data Source=" & subDataSource & _
"; Database=" & subDataBase & "; Integrated Security=" & "SSPI" & "; Persist Security Info=" & subPSI
RemoteSQLcmd.Connection.Open()
RemoteSQLConn = New SqlConnection("Server=" & subDataSource & "; Integrated Security=TRUE" & "; database=" & subDataBase)
RemoteSQLConn.Open()
RemoteSQLtx = RemoteSQLConn.BeginTransaction("MainTranaction")
RemoteSQLcmd.Transaction = RemoteSQLtx
'------------------------- Write in to file stream ---------------'
Dim imImage As Byte() = New Byte(imStream.Length) {}
Dim bytesRead As Integer = imStream.Read(imImage, 0, imStream.Length)
Dim sqlFile As New SqlFileStream(filePathName, txContext, FileAccess.ReadWrite, FileOptions.WriteThrough, 0)
Dim numBytes As Integer = 0
Dim unicode As Encoding = Encoding.GetEncoding(0)
While bytesRead > 0
sqlFile.Write(imImage, 0, bytesRead)
bytesRead = imStream.Read(imImage, 0, imSize)
End While
RemoteSQLtx.Commit()
RemoteSQLcmd.Transaction.Commit()
sqlFile.Close()
Catch FsEx As Exception
MessageBox.Show(FsEx.Message, "Write in SQL File Stream ", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Finally
End Try
'--------------------------------------------------'
Catch ex As Exception
MessageBox.Show(ex.Message, "WriteFileStream ", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Finalize()
Finally
RemoteSQLConn.Close()
imStream.Close()
End Try
End Sub
Do you do all the necessary steps before opening the file stream? Grabbing the context inside a transaction??
Check out this article here for a detailed discussion as to how to do it. Here's another blog post with a VB.NET sample code snippet.
Update: Lefteris, your code snippet is very very hard to read and understand, I did my best, but I'm really not sure. What I'm thinking is that you close the transaction too quickly. You seem to create a connection and a transaction to get the GET_FILESTREAM_TRANSACTION_CONTEXT, but then you close that again before you actually write out your bytes. I think the transaction should span all operations - it should start before you get the transaction context, and it should stay alive for the whole write operation, and only be committed once the whole write operation is done.
I tried to come up with a simpler code snippet to show you - it's in C#, since I couldn't convert it back to VB.NET:
public static void WriteFileStream(byte[] imSource, int imSize)
{
// use your own SQL insert command here instead
const string InsertCmd = "INSERT INTO PhotoAlbum(PhotoId, Description)" +
" VALUES(#PhotoId, #Description)";
using (SqlConnection conn = new SqlConnection(ConnStr))
{
conn.Open();
// create transaction here and let it stay alive!
using (SqlTransaction txn = conn.BeginTransaction())
{
using (SqlCommand cmd = new SqlCommand(InsertCmd, conn, txn))
{
cmd.Parameters.Add("#PhotoId", SqlDbType.Int).Value = photoId;
cmd.Parameters.Add("#Description", SqlDbType.VarChar).Value = desc;
cmd.ExecuteNonQuery();
}
SafeFileHandle handle = GetOutputFileHandle(photoId, txn);
MemoryStream inputStream = new MemoryStream(imSource);
using (FileStream dest = new FileStream(handle, FileAccess.Write))
{
byte[] buffer = new byte[BlockSize];
int bytesRead;
while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
{
dest.Write(buffer, 0, bytesRead);
}
dest.Close();
}
inputStream.Close();
// commit transaction here, after all is done
txn.Commit();
}
conn.Close();
}
}
This is adapted from the blog post SQL Server 2008 FILESTREAM Part 3: OpenSqlFileStream API - check that out by all means!
Related
I made several research but cannot figure out the problem. I can add, remove or edit rows from database in the code. However, I could not refresh the grid.
I mean, when I add new row to gridview, it doesn't appear in it. After I recompile the code it appears. How can I refresh the gridview?
Dim strSQL = "SELECT * FROM FIXEDCARD WHERE STKCODE = '" & TextEdit1.Text & "'"
objCon.Close()
Dim sqlCmd As New SqlCommand(strSQL, objCon)
objCon.Open()
Dim dreader As SqlDataReader
dreader = sqlCmd.ExecuteReader
If dreader.Read Then
'Dim stk_ As String = dreader.GetString(dreader.GetOrdinal("STKCODE")).ToString()
DevExpress.XtraEditors.XtraMessageBox.Show("already exists", "Info!", MessageBoxButtons.OK)
objCon.Close()
Else
dreader.Close()
strSQL = "INSERT INTO FIXEDCARD(STKCODE, STKEXP, AUTHCODE, GRPCODE, SPECODE" & _
", SPECODE2, SPECODE3, SPECODE4, SPECODE5, AMORTRATIO, AMORT," & _
"PURCH, SALES, RETUR, UNIT) VALUES('" & TextEdit1.Text & "'," & _
"'" & TextEdit2.Text & "', '" & TextEdit3.Text & "'," & _
"'" & TextEdit4.Text & "', '" & TextEdit5.Text & "'," & _
"'" & TextEdit6.Text & "', '" & TextEdit7.Text & "'," & _
"'" & TextEdit8.Text & "', '" & TextEdit9.Text & "'," & _
"'" & TextEdit10.Text & "', '" & TextEdit11.Text & "'," & _
"'" & TextEdit12.Text & "', '" & TextEdit13.Text & "'," & _
"'" & TextEdit14.Text & "', '" & TextEdit15.Text & "') "
sqlCmd = New SqlCommand(strSQL, objCon)
sqlCmd.ExecuteNonQuery()
objCon.Close()
End If
RefreshGrid()
Me.Close()
And also I try to write a refresh function that will refresh the gridview when I call it each button. However, I have been making a mistake but could not recognize it.
Public Sub RefreshGrid()
Dim T As New DataTable
Dim strSQL = "Select * From FIXEDCARD"
Dim sqlCmd As New SqlCommand(strSQL, objCon)
T = CType(sqlCmd.ExecuteNonQuery(), System.Data.DataTable)
GridView1.DataSource = T
End Sub
Public Function FillDataSet(query As String, ByVal ParamArray para() As Object) As DataTable
dim _transaction As SqlTransaction
Dim _command As SqlCommand
_command = New SqlCommand(query, yourConnection)
_ds = New DataSet
_sqlda = New SqlDataAdapter(_command )
_command.Transaction = _transaction
For i = 0 To para.Count - 1
_command.Parameters.AddWithValue("#" & i, para(i))
Next
_sqlda.Fill(_ds)
return _ds.tables(0)
End Function
call it
GridControl1.datasource = Nothing
GridControl1.datasource = FillDataSet(YourInsertQuery,YourParam)
GridControl1.RefreshDataSource()
You are using a "Non-Query" in the refresh. This doesn't return a table value.
Try using the SqlCommand.ExecuteReader method.
Reference: https://msdn.microsoft.com/en-us/library/9kcbe65k(v=vs.110).aspx
This is my code for populating the datagridview:
Public Sub generate_list_ftMK(ByVal clbBranch As CheckedListBox, ByVal dtpSDate As DateTimePicker, ByVal dtpEDate As DateTimePicker, ByVal dvList As DataGridView)
Dim ds As DataSet = New DataSet()
Dim da As MySqlDataAdapter
Dim strQ As String = String.Empty
Dim strQ1 As String = String.Empty
Dim colItemCode As String = String.Empty
Dim colReg_Pri As String = String.Empty
Dim colXL_Pri As String = String.Empty
Dim colDel_Date As String = String.Empty
Dim colLabel As String = String.Empty
Dim colDays As String = String.Empty
Dim PCT As String = String.Empty
Try
If con.State = ConnectionState.Open Then con.Close()
con.Open()
'clear the dataset
ds.Tables.Clear()
'loop for all branches
For i = 0 To clbBranch.CheckedItems.Count - 1
Application.DoEvents()
'get the Branch Code
chkBranch = clbBranch.CheckedItems(i).ToString
'select item code and price where JO_TYPE is JO
strQ = "select " & _
"CONCAT(a.STYLE_CODE, '-', LPAD((a.JO_NO), 4, '0'), '-', d.LINE_NO) as Item_Code, " & _
"DATE_FORMAT(c.DEL_DATE, '%Y-%m-%d') as DEL_DATE, " & _
"d.REG_PRI as Reg_Price, " & _
"d.XL_PRI as XL_Price, " & _
"a.LABEL_CODE, " & _
"a.STYLE_CODE, " & _
"LPAD((a.JO_NO), 4, '0')," & _
"d.LINE_NO, " & _
"DATEDIFF('" & Format(Date.Now, "yyyy-MM-dd") & "',DATE_FORMAT(c.DEL_DATE, '%Y-%m-%d')) as Days " & _
"from " & _
"jo_hdr as a " & _
"inner join " & _
"branch as b ON a.BRNCH_CODE = b.BRNCH_CODE " & _
"inner join " & _
"dr_hdr as c ON c.TRAN_REF = a.TRAN_NO " & _
"inner join " & _
"dr_det as d ON d.DR_NO = c.DR_NO " & _
"where " & _
"c.DEL_DATE BETWEEN '" & dtpSDate.Text & "' AND '" & dtpEDate.Text & "' " & _
"and a.LABEL_CODE = '" & FtMK_Code & "' " & _
"and a.BRNCH_CODE = '" & chkBranch & "' " & _
"and SUBSTRING(d.REG_PRI,LENGTH(d.REG_PRI) - 1,2) = 75 " & _
"and SUBSTRING(d.REG_PRI,LENGTH(d.XL_PRI) - 1,2) = 75 "
cmd = New MySqlCommand(strQ, con)
da = New MySqlDataAdapter(cmd)
ds = New DataSet
da.Fill(ds, "tblJO")
For r = 0 To ds.Tables("tblJO").Rows.Count - 1
Application.DoEvents()
colItemCode = ds.Tables("tblJO").Rows(r)(0).ToString
colDel_Date = ds.Tables("tblJO").Rows(r)(1).ToString
colReg_pri = ds.Tables("tblJO").Rows(r)(2).ToString
colXL_pri = ds.Tables("tblJO").Rows(r)(3).ToString
colLabel = ds.Tables("tblJO").Rows(r)(4).ToString
sumStyle = ds.Tables("tblJO").Rows(r)(5).ToString
sumJO = ds.Tables("tblJO").Rows(r)(6).ToString
sumNo = ds.Tables("tblJO").Rows(r)(7).ToString
colDays = ds.Tables("tblJO").Rows(r)(8).ToString
'for the number to decimal
colReg_pri = FormatNumber(colReg_pri, 2)
colXL_pri = FormatNumber(colXL_pri, 2)
'get the total quantity and the branch quantity
get_TotalQty_and_BranchQty()
'if the branch quantity is zero
If branchQty = 0 Then
PCT = "N\A"
Else
'compute the percent sold
PCT = totalQty
End If
dvList.Rows.Add(False, colItemCode, PCT, colDel_Date, Format(Date.Now, "yyyy-MM-dd"), colDays, _
colReg_Pri, colXL_Pri, colLabel, "JO", dtpSDate.Text, dtpEDate.Text, _
clbBranch.CheckedItems(i).ToString, mkType)
Next
Next
Catch ex As Exception
MsgBox(ex.Message)
Finally
con.Close()
End Try
End Sub
I have use indexes in my tables, and also add application.doevents. Is there anything I could do to make the populating in the datagridview faster with this code? I'm trying to search about making the datagridview doublebuffered because I read it online that I will make the datagridview faster but I have no idea how to use it.
Any help will be appreciated, Thank you.
Your issue is using
Application.DoEvents
I'm not going to get into the details of why this is bad practice, try using a Background Worker instead.
I am looping through a text file and reading and then parsing each line.. then, inserting into a sql server. The problem is, I am getting every other line. Any thoughts?
I read another post on here that is similar and it says that that person was calling the ReadLine twice... which makes sense. I just cannot spot where it is happening.
Private Sub btnUpdateRSR_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdateRSR.Click
' Get RSR File
lblStatus.Text = "Getting RSR File"
Application.DoEvents()
If chkDLRSR.Checked = True Then
ftpGetRSR()
End If
Application.DoEvents()
lblStatus.Text = "Got RSR File"
' Whack existing data
killRSRData()
' Run Update of Invnetory
Dim sCmd As New SqlCommand()
Dim fp As StreamReader
Dim MyFile As String = String.Empty
Dim dblRecordCount As Double
Try
'Open file with StreamReader
fp = File.OpenText("c:\ct_info\rsr.txt")
Catch err As Exception
lblStatus.Text = "File Read Failed! Reason: " & err.ToString()
End Try
sCmd.Connection = New System.Data.SqlClient.SqlConnection("Data Source=vortx-sql01.vortx.com;Initial Catalog=expresspolicesupply;Persist Security Info=True;User ID=expresspolicesupply;Password=blahblah")
sCmd.Connection.Open()
Dim strArr() As String
While String.IsNullOrEmpty(fp.ReadLine) = False ' fp.ReadLine.IsNullOrEmpty = False
MyFile = fp.ReadLine
strArr = MyFile.Split(";")
'Show what is in the second position of the array.
'MessageBox.Show(strArr(1))
Try
Dim RSRstocknumber As String = strArr(0)
Dim UPCcode As String = strArr(1)
Dim ProductDescription As String = Replace(strArr(2), "'", """")
Dim DepartmentNumber As String = strArr(3)
Dim ManufacturerID As String = strArr(4)
Dim RetailPrice As String = strArr(5)
Dim RSRRegularPrice As String = strArr(6)
Dim Weight As String = strArr(7)
Dim InventoryQuantity As String = strArr(8)
Dim Model As String = Replace(strArr(9), "'", """")
Dim FullManufacturerName As String = Replace(strArr(10), "'", """")
Dim ManufacturerPartNo As String = strArr(11)
Dim AllocatedCloseoutDeleted As String = strArr(12)
Dim ExpandedProductDescription As String = Replace(strArr(13), "'", """")
Dim ImageName As String = strArr(14)
lblStatusPrevious.Text = "Previous one: " & lblStatus.Text
lblStatus.Text = strArr(0)
Application.DoEvents()
With sCmd
.CommandText = "INSERT into rsr (rsrstocknumber, upccode, productDescription, departmentnumber, ManufacturerID, RetailPrice, RSRRegularPrice, Weight, InventoryQuantity, Model, FullManufacturerName, ManufacturerPartNo, AllocatedCloseoutDeleted, ExpandedProductDescription, ImageName) " & _
" Values('" & RSRstocknumber & "', '" & UPCcode & "', '" & ProductDescription & "', '" & DepartmentNumber & "', '" & ManufacturerID & "', '" & _
RetailPrice & "', '" & RSRRegularPrice & "', '" & Weight & "', '" & InventoryQuantity & "', '" & Model & "', '" & FullManufacturerName & "', '" & ManufacturerPartNo & "', '" & _
AllocatedCloseoutDeleted & "', '" & ExpandedProductDescription & "','" & ImageName & "');"
'MessageBox.Show(sCmd.CommandText.ToString)
.CommandType = CommandType.Text
.ExecuteNonQuery()
' Update record counter
dblRecordCount = dblRecordCount + 1
lblRecordCount.Text = dblRecordCount
Application.DoEvents()
End With
Catch ex As Exception
lblErrorLabel.Text = "Error on thown on " & lblRecordCount.Text
'MessageBox.Show("Error occurred! Details: " & ex.Message)
End Try
End While
fp.Close() 'Close file with StreamReader
sCmd.Connection.Close()
lblStatus.Text = "Updating website for RSR"
lblStatusPrevious.Text = ""
spUpdateRSR()
lblStatus.Text = "Done"
lblStatusPrevious.Text = ""
End Sub
That's because you're calling ReadLine() twice.
Every other line gets swallowed by String.IsNullOrEmpty(fp.ReadLine) = False.
You are calling ReadLine twice.
While String.IsNullOrEmpty(fp.ReadLine) = False ' fp.ReadLine.IsNullOrEmpty = False
MyFile = fp.ReadLine
Once in the IsNullOrEmpty check and once on MyFile = fp.ReadLine
This line is reading the file, and then you read the next one in the while loop:
While String.IsNullOrEmpty(fp.ReadLine) = False
You are calling it twice :)
You need to use this to check whether it is the end of the file:
While fp.EndOfStream = False
I have create my code in order to write images in a remote sql server
All the details of accessing and writing are fine until now, including the system account right now i'm in the command of:
SqlFileStream = New SqlFileStream(filePathName, fileToken, FileAccess.Write)
and when i'm trying to execute it the Server returns the error 'Access denied' I have try all the posible ( those which i know) combinations to overcome this error but nothing Please give me the best assistance you may have
I've put a sniffer in my PC to lookup the packages between Server and my PC, so here what i got: the first addres is the Server address and the second is my PC address.
*"10.93.1.29","10.93.1.10","SMB","Tree Connect AndX Request, Path: \DEVELOPER\SQLEXPRESS "
"10.93.1.10","10.93.1.29","SMB","Tree Connect AndX Response"
"10.93.1.29","10.93.1.10","SMB","Trans2 Request, QUERY_PATH_INFO, Query File Basic Info, Path: \v1\RemoteDB\dbo\tPImages\tPImages_Image\DB6F1B11-2FAF-4326-8E44-FBA71DA94CEC\b8010a0f1aaf47c1888aab2e830dff43"
"10.93.1.10","10.93.1.29","SMB","Trans2 Response, QUERY_PATH_INFO, Error: STATUS_ACCESS_DENIED"
"10.93.1.29","10.93.1.10","SMB","NT Trans Request, NT CREATE"
"10.93.1.10","10.93.1.29","SMB","NT Trans Response, NT CREATE, FID: 0x0000, Error: STATUS_ACCESS_DENIED"*
I dare to say that this error comes from the Windows program when the SQL 2008 tries to write some DATA to the filies which creates on C:\sqlRemData..... (but finally i'm not sure even for that)
PLEASE if anyone knows?
Try this..
public static bool SetAcl(string filename, string account)
{
FileSystemAccessRule rule = new FileSystemAccessRule(account, FileSystemRights.Write, AccessControlType.Allow);
PermissionSet fp = new PermissionSet(PermissionState.Unrestricted);
fp.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read, new string[] { filename }));
fp.AddPermission(new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.PathDiscovery, new string[] { filename }));
fp.Assert();
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(System.IO.Path.GetDirectoryName(filename));
bool what = false;
DirectorySecurity security = di.GetAccessControl();
security.ModifyAccessRule(AccessControlModification.Add, rule, out what);
di.SetAccessControl(security);
return what;
}
Public Sub WriteFileStream(ByVal imSource As Byte(), ByVal imSize As Integer, ByVal imTy As Type, ByVal sender As Object, ByVal e As EventArgs)
Dim subProvider As String = Nothing
Dim subDataSource As Object = Nothing
Dim subUid As String = Nothing
Dim subPwd As String = Nothing
Dim subDataBase As String = Nothing
Dim subPSI As Boolean = Nothing
Dim ParamXML() As String = Nothing
Dim TypeOfServer As String = "Remote"
Dim imParam(3) As String
Dim imTable(1) As String
Dim RemoteSQLcmd As SqlCommand = New SqlCommand
Dim tokenReader As SqlDataReader
'-------------------------------------------------'
Dim AbsRecord As Int64 = 0
Dim VarString(10) As String
Dim VarInt(10) As Integer
'-------------------------------------------------'
ParamXML = Split(loadXmlFile(TypeOfServer, sender, e), "|")
subUid = ParamXML(3)
subProvider = ParamXML(0)
subDataSource = ParamXML(1)
subDataBase = ParamXML(2)
subPwd = ParamXML(4)
subPSI = ParamXML(5)
Dim SchemaID As String = Convert.ToInt16(ParamXML(8))
Dim SchemaName As String = Nothing
If SchemaID = 1 Then
SchemaName = subUid
ElseIf SchemaID = 2 Then
SchemaName = "dbo"
ElseIf SchemaID = 0 Then
SchemaName = "dbo"
End If
'-------------------------------------------------'
imTable(0) = "tPDetails"
imTable(1) = "tPImages"
Try
imParam(0) = Me.TextBox1.Text.Trim.ToString 'Name'
imParam(1) = Me.TextBox2.Text.Trim.ToString 'Code'
imParam(2) = Me.TextBox3.Text.Trim.ToString 'Price'
imParam(3) = Me.TextBox4.Text.Trim.ToString 'Comments'
'========================================================'
If RemoteSQLConn.State = ConnectionState.Open Then RemoteSQLConn.Close()
SQL_Connection(TypeOfServer, TypeOfServer & "Conn.xml", sender, e)
RemoteSQLConn.open()
'----------------------'
Dim imHolder As Image = Image.FromStream(imStream)
Dim imHeight As Integer = imHolder.Height
Dim imWidth As Integer = imHolder.Width
Dim imLength As Integer = imHolder.PropertyItems.Length
Dim imType As Type = imTy
'----------------------'
Dim FirstColumnNames As String = _
imTable(0) & "_Code, " & _
imTable(0) & "_Price, " & _
imTable(0) & "_Title, " & _
imTable(0) & "_Type, " & _
imTable(0) & "_Height, " & _
imTable(0) & "_Width, " & _
imTable(0) & "_Stock, " & _
imTable(0) & "_Comments "
Dim FirstFieldsValues As String = "'" & imParam(1) & "', '" & _
imParam(2) & "', '" & _
imParam(0) & "', '" & _
imType.ToString & "', '" & _
imHeight & "', '" & _
imWidth & "', '" & _
"0', '" & _
imParam(3) & "' "
'--------------------------------------------'
RemoteSQLcmd = New SqlCommand("INSERT INTO " & _
SchemaName & "." & imTable(0) & " (" & FirstColumnNames & ") VALUES (" & FirstFieldsValues & ") ", RemoteSQLConn)
RemoteSQLcmd.ExecuteNonQuery()
'--------------------------------------------------'
RemoteSQLcmd = New SqlCommand("SELECT * FROM " & SchemaName & "." & imTable(0) & _
" WHERE " & imTable(0) & "_Code = " & "'" & imParam(1) & "'", RemoteSQLConn)
AbsRecord = RemoteSQLcmd.ExecuteScalar
'--------------------------------------------------'
RemoteSQLcmd = New SqlCommand("INSERT INTO " & SchemaName & "." & imTable(1) & " VALUES (newid(), " & AbsRecord & ", CAST('' as varbinary(max)))", RemoteSQLConn)
RemoteSQLcmd.ExecuteNonQuery()
'--------------------------------------------------'
RemoteSQLcmd = New SqlCommand("SELECT " & imTable(1) & "_Image.PathName() FROM " & _
SchemaName & "." & imTable(1) & " WHERE " & imTable(1) & "_" & imTable(0) & "_ID = " & AbsRecord, RemoteSQLConn)
Dim filePathName As String = Nothing
Dim pathObj As Object = RemoteSQLcmd.ExecuteScalar()
'-------------------------------------------------- Path Name '
If Not pathObj.Equals(DBNull.Value) Then
filePathName = DirectCast(pathObj, String)
Else
Throw New System.Exception("Image.PathName() failed to read the path name for the Image column.")
End If
'-------------------------- GET_FILESTREAM_TRANSACTION_CONTEXT()'
Dim RemoteSQLtx As SqlTransaction = RemoteSQLConn.BeginTransaction("MainTranaction")
RemoteSQLcmd.Transaction = RemoteSQLtx
RemoteSQLcmd = New SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", RemoteSQLConn, RemoteSQLtx)
Dim tokenObject As Object = RemoteSQLcmd.ExecuteScalar()
'-------------------------------------- File Token '
tokenReader = RemoteSQLcmd.ExecuteReader(CommandBehavior.SingleRow)
tokenReader.Read()
Dim txContext As SqlBinary = DirectCast(tokenObject, Byte())
tokenReader.Close()
'-----------------------------------------------'
Try
'-------------------- Closing all connections'
If RemoteSQLConn.State = ConnectionState.Open Then RemoteSQLConn.Close()
If RemoteSQLcmd.Connection.State = ConnectionState.Open Then RemoteSQLcmd.Connection.Close()
If RemoteConnInfo.State = ConnectionState.Open Then RemoteConnInfo.Close()
RemoteSQLtx.Dispose()
'---------- Opening all connections for Indegrated Security'
ChangeLoginPerson("PRINCIDEVEL\Administrator")
RemoteConnInfo.ConnectionString = "Provider=" & subProvider & "; Data Source=" & subDataSource & _
"; Database=" & subDataBase & "; Integrated Security=" & "SSPI" & "; Persist Security Info=" & subPSI
RemoteSQLcmd.Connection.Open()
RemoteSQLConn = New SqlConnection("Server=" & subDataSource & "; Integrated Security=TRUE" & "; database=" & subDataBase)
RemoteSQLConn.Open()
RemoteSQLtx = RemoteSQLConn.BeginTransaction("MainTranaction")
RemoteSQLcmd.Transaction = RemoteSQLtx
'------------------------- Write in to file stream ---------------'
Dim imImage As Byte() = New Byte(imStream.Length) {}
Dim bytesRead As Integer = imStream.Read(imImage, 0, imStream.Length)
Dim sqlFile As New SqlFileStream(filePathName, txContext, FileAccess.ReadWrite, FileOptions.WriteThrough, 0)
Dim numBytes As Integer = 0
Dim unicode As Encoding = Encoding.GetEncoding(0)
While bytesRead > 0
sqlFile.Write(imImage, 0, bytesRead)
bytesRead = imStream.Read(imImage, 0, imSize)
End While
RemoteSQLtx.Commit()
RemoteSQLcmd.Transaction.Commit()
sqlFile.Close()
Catch FsEx As Exception
MessageBox.Show(FsEx.Message, "Write in SQL File Stream ", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Finally
End Try
'--------------------------------------------------'
Catch ex As Exception
MessageBox.Show(ex.Message, "WriteFileStream ", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Finalize()
Finally
RemoteSQLConn.Close()
imStream.Close()
End Try
End Sub
Dear Folk's i'm using the following code in order to send the bytes of a picture into the stream table:
Dim FirstColumnNames As String = imTable(0) & "_Code, " & imTable(0) & "_Price, " & imTable(0) & "_Title, " & imTable(0) & "_Type, " & imTable(0) & "_Height, " & imTable(0) & "_Width, " & imTable(0) & "_Comments "
Dim FirstFieldsValues As String = "'" & imParam(1) & "', '" & imParam(2) & "', '" & imParam(0) & "', '" & imType.ToString & "', '" & imHeight & "', '" & imWidth & "', '" & imParam(3) & "' "
RemoteSQLcmd = New SqlCommand("INSERT INTO " & imTable(0) & " (" & FirstColumnNames & ") VALUES (" & FirstFieldsValues & ") ", RemoteSQLConn, RemoteSQLtx)
RemoteSQLcmd.ExecuteNonQuery()
RemoteSQLcmd = New SqlCommand("SELECT * FROM " & imTable(0) & " WHERE " & imTable(0) & "_Code = " & "'" & imParam(1) & "'", RemoteSQLConn, RemoteSQLtx)
AbsRecord = RemoteSQLcmd.ExecuteScalar
Dim imGUID As Guid = Guid.NewGuid()
Dim SecondColumnNames As String = imTable(1) & "_" & imTable(0) & "_ID , " & imTable(1) & "_GUID "
Dim SecondFieldsValues As String = "'" & AbsRecord & "', '" & imGUID.ToString & "'"
RemoteSQLcmd = New SqlCommand("INSERT INTO " & imTable(1) & " (" & SecondColumnNames & ") VALUES (" & SecondFieldsValues & ") ", RemoteSQLConn, RemoteSQLtx)
RemoteSQLcmd.ExecuteNonQuery()
RemoteSQLcmd = New SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT() " & "FROM " & imTable(1) & " WHERE " & imTable(1) & "_" & imTable(0) & "_ID = " &AbsRecord, RemoteSQLConn, RemoteSQLtx)
RemoteSQLcmd.Parameters.Add("#" & imTable(1) & "_GUID", SqlDbType.UniqueIdentifier).Value = imGUID
Dim tokenObject As Object = RemoteSQLcmd.ExecuteScalar()
tokenReader = RemoteSQLcmd.ExecuteReader(CommandBehavior.SingleRow)
tokenReader.Read()
filePathName = tokenReader.GetSqlString(1)
fileToken = DirectCast(tokenReader(3), Byte())
tokenReader.Close()
Dim sqlFile As SqlFileStream = New SqlFileStream(filePathName.Value, fileToken.Value, FileAccess.Write)
The tables have the fllowing stracture
Thats the First Table:
myCommand = New SqlCommand("CREATE TABLE " & TablesStat(0, 0) & _
" (" & TablesStat(0, 0) & "_ID int NOT NULL PRIMARY KEY IDENTITY(1,1), " & TablesStat(0, 0) & "_Code varchar(20) NULL, " & TablesStat(0, 0) & "_Price money NULL, " & TablesStat(0, 0) & "_Title varchar(50) NULL, " & TablesStat(0, 0) & "_Type sql_variant NULL, " & TablesStat(0, 0) & "_Height int NULL, " & TablesStat(0, 0) & "_Width int NULL, " & TablesStat(0, 0) & "_Comments nvarchar(MAX) NULL)", RemoteSQLConn)
myCommand.ExecuteNonQuery()
End the second table is:
myCommand = New SqlCommand("CREATE TABLE " & TablesStat(1, 0) & _
" (" & TablesStat(1, 0) & "_ID int NOT NULL PRIMARY KEY IDENTITY(1,1), " & TablesStat(1, 0) & "_GUID UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE , " & TablesStat(1, 0) & "_" & TablesStat(0, 0) & "_ID int FOREIGN KEY REFERENCES " & TablesStat(0, 0) & " (" & TablesStat(0, 0) & "_ID) NOT NULL, " & TablesStat(1, 0) & "_Image varbinary(MAX) FILESTREAM NULL ) ", RemoteSQLConn)
myCommand.ExecuteNonQuery()
My problem comes when i'm trying to read the 'filePathName' and the 'fileToken'
the privious SELECT GET_FILESTREAM.... return me only one colomn to read the colomn 0 which has the GUID in binary format
I know i'm doing something wrong but i don't know what
My issue is that i'm not geting th 'filePathName' and the fileToken'
is there anybody to assist me?
Look Max
I did what you told me but nothing
RemoteSQLcmd = New SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()", RemoteSQLConn, RemoteSQLtx)
Dim tokenObject As Object = RemoteSQLcmd.ExecuteScalar()
tokenReader = RemoteSQLcmd.ExecuteReader(CommandBehavior.SingleRow)
tokenReader.Read()
fileToken = DirectCast(tokenReader(1), Byte())
filePathName = tokenReader.GetSqlString(3)
And the Transaction starts far upper of this command
And never stops
Dim imGUID As Guid = Guid.NewGuid()
Dim imImage As Byte() = New Byte(imStream.Length) {}
Dim bytesRead As Integer = imStream.Read(imImage, 0, imStream.Length)
Dim SecondColumnNames As String = _
imTable(1) & "_GUID, " & _
imTable(1) & "_" & imTable(0) & "_ID"
Dim SecondFieldsValues As String = "'" & imGUID.ToString & "', '" & AbsRecord & "'"
RemoteSQLcmd = New SqlCommand("INSERT INTO " & imTable(1) & _
" (" & SecondColumnNames & ") VALUES (" & SecondFieldsValues & ")", RemoteSQLConn, RemoteSQLtx)
RemoteSQLcmd.Parameters.Add("#" & imTable(1) & "_GUID", SqlDbType.UniqueIdentifier).Value = imGUID
RemoteSQLcmd.Parameters.Add("#" & imTable(1) & "_Image", SqlDbType.Image).Value = imImage
RemoteSQLcmd.ExecuteNonQuery()
RemoteSQLcmd = New SqlCommand("SELECT GET_FILESTREAM_TRANSACTION_CONTEXT() FROM " & imTable(1), RemoteSQLConn, RemoteSQLtx)
Dim tokenObject As Object = RemoteSQLcmd.ExecuteScalar()
tokenReader = RemoteSQLcmd.ExecuteReader(CommandBehavior.SingleRow)
tokenReader.Read()
As you can see i put again the FROM claus.
But please look what i'm receiving in the reader
tokenReader.Depth 0
tokenReader.FieldCount 1
tokenReader.HasRows True
tokenReader.IsClosed False
tokenReader.Item In order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
tokenReader.RecordsAffected -1
As you can see here i have only one column to read and nothing else
I really don't know if that is helpful but anyway i put it there
tokenReader.VisibleFieldCount 1
MSDN said you should
do this inside TRANSACTION
do "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()" without any "FROM TABLE"
There is a sample in Working with FILESTREAM using VB .NET By Yan Pan:
' Obtain a transaction context. All FILESTREAM BLOB operations occur '
' within a transaction context to maintain data consistency. '
Dim transaction As SqlTransaction =
sqlConnection.BeginTransaction("mainTranaction")
sqlCommand.Transaction = transaction
sqlCommand.CommandText = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()"
Dim obj As Object = sqlCommand.ExecuteScalar()
Dim txContext As Byte() = Nothing
If Not obj.Equals(DBNull.Value) Then
txContext = DirectCast(obj, Byte())
Else
Throw New System.Exception("GET_FILESTREAM_TRANSACTION_CONTEXT() failed")
End If
' Obtain a handle that can be passed to the Win32 FILE APIs. '
Dim sqlFileStream As New SqlFileStream(filePath, txContext, FileAccess.Write)
' Converting the image to a byte array. '
' Please change C:\Spire.jpg to your image file path. '
Dim byteImg As Byte()
byteImg = File.ReadAllBytes("C:\Spire.jpg")
'Write the image file to the FILESTREAM BLOB. '
sqlFileStream.Write(byteImg, 0, byteImg.Length)
' Close the FILESTREAM handle. '
sqlFileStream.Close()
' Commit the write operation that was performed on the FILESTREAM BLOB. '
sqlCommand.Transaction.Commit()
Try to change your code according to sample:
filePath variable is initiated before opening file transaction
there is a simple "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()" command for file context
there is a null check and DirectCast(obj, Byte()) to cast value into byte
not sure if it make difference, but SqlFileStream is opening with txContext, not txContext.Value
UPDATE
How I understand the problem:
filePathName - filename from where we will open filestream. this value should be selected from some field of some your table. If you don't know what [table].[field] it is, see inside db table values and find where file path are saved.
fileToken - filestream transaction context. should be selected in separate command execute, and casted to Byte.
SQL Injections means when you do things like
RemoteSQLcmd = New SqlCommand("SELECT * FROM " & imTable(0) & " WHERE "
& imTable(0) & "_Code = " & "'" & imParam(1)
& "'", RemoteSQLConn, RemoteSQLtx)
in codebehind and imParam is an URL parameter value than someone may play a bad joke with you putting "'; DROP TABLE users;" in it which may resolve into
SELECT * FROM table WHERE table_Code = ''; DROP TABLE users;