问题
My objective is to store DICOM files in SQL Server with some ID, and when user wants that DICOM file he can download from the SQL Server using its corresponding ID. The file should not change its originality while storing into the server and also when retrieving from the SQL Server. I am using Varbinary(max) data-type to store the byte array of the DICOM file in SQL Server. I am converting the memory stream of the DICOM file into byte array and then storing into the SQL server as mentioned below:
using (MemoryStream memStream = new MemoryStream())
{
using (FileStream fileStream = File.Open(txtDICOMFilePath.Text, FileMode.Open))
{
// Copy the file stream to memory stream.
fileStream.CopyTo(memStream);
}
int intL = Convert.ToInt32(memStream.Length);
byte[] objData = new byte[intL];
}
//Set insert query
string qry = "insert into ImagesStore (ID,ImageData) values(@ID, @ImageData)";
//Initialize SqlCommand object for insert.
SqlCommand SqlCom = new SqlCommand(qry, CN);
//We are passing Original Image Path and Image byte data as sql parameters.
SqlCom.Parameters.Add(new SqlParameter("@ID", (object)txtUniqueID.Text));
SqlCom.Parameters.Add(new SqlParameter("@ImageData", (object)objData));
//Open connection and execute insert query.
CN.Open();
SqlCom.ExecuteNonQuery();
CN.Close();
Problem: While storing the Image it stores correctly(Without any exception) but while retrieving it not gives the exact data. can any body help me in storing the DICOM file in SQL server without any loss?
Update: Below is my code for download the DICOM file.
qry = "select ImageData from ImagesStore where ID= @ID";
//Initialize SqlCommand object for insert.
SqlCom = new SqlCommand(qry, CN);
SqlCom.Parameters.Add("@ID", SqlDbType.Int).Value = (object)txtUniqueID.Text;
SqlDataAdapter adp = new SqlDataAdapter(SqlCom);
DataTable dt = new DataTable();
try
{
if (CN.State == ConnectionState.Closed)
CN.Open();
adp.Fill(dt);
if (dt.Rows.Count > 0)
{
MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["ImageData"]);
//Logic to save the file
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
回答1:
Declare byte[] objData outside and before using block and try to set size of byte array:
byte []objData=null;
using (MemoryStream memStream = new MemoryStream())
{
using (FileStream fileStream = File.Open(txtDICOMFilePath.Text, FileMode.Open))
{
fileStream.CopyTo(memStream);
}
int intL = Convert.ToInt32(memStream.Length);
objData = new byte[intL];
memStream.Read(objData,0,objData.Length);
}
SqlCom.Parameters.Add("@ImageData",SqlDb.Image,objData.Length).Value=objData;
EDIT: To read data from ImagesStore
string qry = "select * From ImagesStore";
using(SqlConnection Cn=new SqlConnect(CnStr))
{
using(SqlCommand SqlCom = new SqlCommand(qry, CN))
{
Cn.Open();
using(SqlDataReader dr=SqlCom.ExecuteReader())
{
while(dr.Read())
{
string path="x:\\folder\\" + dr[0] + ".png";
byte []bytes=(byte[])dr[1];
System.IO.File.WriteAllBytes(path,bytes);
}
}
}
}
Alternatively you can use DataAdapter/DataTable (code in OP)
if (dt.Rows.Count > 0)
{
foreach(DataRow row in dt.Rows)
{
string path="x:\\folder\\" + row[0] + ".png";
byte []bytes=(byte[])row[1];
System.IO.File.WriteAllBytes(path,bytes);
}
}
来源:https://stackoverflow.com/questions/11741558/how-to-store-and-retrieve-dicom-files-in-sql-server-using-c-sharp