arshpisheh
سه شنبه 23 مهر 1392, 17:17 عصر
با سلام به تمامی اساتید و دوستان محترم.
ما پروژه ای داریم که قرار است فایلهای اسکن شده را بر روی سرور به روش SQL File Stream ذخیره نماید. این برنامه گاهی به طور صحیح این عملیات را انجام داده اما گاهی هنگام ذخیره روی سرور با خطای زیر مواجه میشویم.که تعداد دادن این خطا بسیار بالا رفته است.پیشاپیش از راهنماییهای شما اساتید و دوستان سپاسگذاریم.
expMessage:System.ArgumentException: Invalid handle.
Parameter name: handle
at System.IO.FileStream..ctor(SafeFileHandle handle,
FileAccess access, Int32 bufferSize, Boolean isAsync)
کدهای مربوطه به شرح ذیل است:
SqlCommand selectCommand = new SqlCommand("INSERT BimarScanImageItems (GuidBimarScanImageItems,GuidBimarDocScans" +
",ImageItemNumber,docTypeCode,docTypeName,GuidInser tUser,InsertDate,InsertTime,ImageItem) OUTPUT Inserted.ImageItem.PathName() " +
"VALUES('" + GuidBimarScanImageItems + "','" + GuidBimarDocScans + "'" +
",'" + ImageItemNumber + "'," + docTypeCode + ",'" + docTypeName + "','" + GuidInsertUser + "','" + InsertDate + "','" + InsertTime + "',CAST('' AS VARBINARY(MAX)))");
selectCommand.Connection = sqlConn;
selectCommand.CommandType = CommandType.Text;
String filePath = "";
Object pathObj = selectCommand.ExecuteScalar();
try
{
if (pathObj != DBNull.Value)
filePath = (string)pathObj;
selectCommand.CommandText = "BEGIN TRANSACTION";
selectCommand.CommandType = CommandType.Text;
selectCommand.ExecuteNonQuery();
selectCommand.CommandText = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()";
object obj = selectCommand.ExecuteScalar();
byte[] txContext = null;
UInt32 contextLength = 0;
if (obj != DBNull.Value)
{
txContext = (byte[])obj;
contextLength = (UInt32)txContext.Length;
}
else
{
string message = "GET_FILESTREAM_TRANSACTION_CONTEXT() failed";
//Throw New System.Exception(message)
}
SafeFileHandle handle;
handle = OpenSqlFilestream(filePath, DESIRED_ACCESS_READWRITE, SQL_FILESTREAM_OPEN_NO_FLAGS, txContext, contextLength, 0);
if (handle.IsInvalid)
{
string message = "";
string.Format(message, "OpenSqlFilestream() failed" + " GetLastError() = {0:X}", GetLastError());
}
byte[] buffer = new byte[512];
Byte[] bytes;
//string pa = @"C:\Documents and Settings\hamidi.FHM\My Documents\My Pictures\agt_action_fail_256.png";
FileStream input = new FileStream(path, FileMode.Open, FileAccess.Read);
//int inputLen = (int)input.Length;
//input.Flush();
//input.Close();
try
{
FileStream filestream = new FileStream(handle, FileAccess.ReadWrite, buffer.Length, false);
BinaryReader reader = new BinaryReader(input);
//Dim bytes() As Byte
bytes = reader.ReadBytes((int)input.Length);
filestream.Write(bytes, 0, (int)input.Length);
filestream.Seek(0, SeekOrigin.Begin);
filestream.Close();
filestream.Dispose();
}
ما پروژه ای داریم که قرار است فایلهای اسکن شده را بر روی سرور به روش SQL File Stream ذخیره نماید. این برنامه گاهی به طور صحیح این عملیات را انجام داده اما گاهی هنگام ذخیره روی سرور با خطای زیر مواجه میشویم.که تعداد دادن این خطا بسیار بالا رفته است.پیشاپیش از راهنماییهای شما اساتید و دوستان سپاسگذاریم.
expMessage:System.ArgumentException: Invalid handle.
Parameter name: handle
at System.IO.FileStream..ctor(SafeFileHandle handle,
FileAccess access, Int32 bufferSize, Boolean isAsync)
کدهای مربوطه به شرح ذیل است:
SqlCommand selectCommand = new SqlCommand("INSERT BimarScanImageItems (GuidBimarScanImageItems,GuidBimarDocScans" +
",ImageItemNumber,docTypeCode,docTypeName,GuidInser tUser,InsertDate,InsertTime,ImageItem) OUTPUT Inserted.ImageItem.PathName() " +
"VALUES('" + GuidBimarScanImageItems + "','" + GuidBimarDocScans + "'" +
",'" + ImageItemNumber + "'," + docTypeCode + ",'" + docTypeName + "','" + GuidInsertUser + "','" + InsertDate + "','" + InsertTime + "',CAST('' AS VARBINARY(MAX)))");
selectCommand.Connection = sqlConn;
selectCommand.CommandType = CommandType.Text;
String filePath = "";
Object pathObj = selectCommand.ExecuteScalar();
try
{
if (pathObj != DBNull.Value)
filePath = (string)pathObj;
selectCommand.CommandText = "BEGIN TRANSACTION";
selectCommand.CommandType = CommandType.Text;
selectCommand.ExecuteNonQuery();
selectCommand.CommandText = "SELECT GET_FILESTREAM_TRANSACTION_CONTEXT()";
object obj = selectCommand.ExecuteScalar();
byte[] txContext = null;
UInt32 contextLength = 0;
if (obj != DBNull.Value)
{
txContext = (byte[])obj;
contextLength = (UInt32)txContext.Length;
}
else
{
string message = "GET_FILESTREAM_TRANSACTION_CONTEXT() failed";
//Throw New System.Exception(message)
}
SafeFileHandle handle;
handle = OpenSqlFilestream(filePath, DESIRED_ACCESS_READWRITE, SQL_FILESTREAM_OPEN_NO_FLAGS, txContext, contextLength, 0);
if (handle.IsInvalid)
{
string message = "";
string.Format(message, "OpenSqlFilestream() failed" + " GetLastError() = {0:X}", GetLastError());
}
byte[] buffer = new byte[512];
Byte[] bytes;
//string pa = @"C:\Documents and Settings\hamidi.FHM\My Documents\My Pictures\agt_action_fail_256.png";
FileStream input = new FileStream(path, FileMode.Open, FileAccess.Read);
//int inputLen = (int)input.Length;
//input.Flush();
//input.Close();
try
{
FileStream filestream = new FileStream(handle, FileAccess.ReadWrite, buffer.Length, false);
BinaryReader reader = new BinaryReader(input);
//Dim bytes() As Byte
bytes = reader.ReadBytes((int)input.Length);
filestream.Write(bytes, 0, (int)input.Length);
filestream.Seek(0, SeekOrigin.Begin);
filestream.Close();
filestream.Dispose();
}