如何使用C#在SQL Server中存储和检索DICOM文件 [英] How to store and retrieve DICOM files in SQL Server using C#

查看:125
本文介绍了如何使用C#在SQL Server中存储和检索DICOM文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将DICOM文件存储在具有某些ID的SQL Server中,当用户想要该DICOM文件时,他可以使用其相应ID从SQL Server下载。该文件在存储到服务器中以及从SQL Server检索时,不应更改其原始性。我正在使用 Varbinary(max)数据类型在SQL Server中存储DICOM文件的字节数组。我将DICOM文件的内存流转换为字节数组,然后按如下所述存储到SQL Server:

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();

问题:
在正确存储它存储的图像的同时(没有任何例外),但在检索时并不能提供确切的数据。谁能帮助我在SQL Server中存储DICOM文件而不会造成任何损失?

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);
 }


推荐答案

声明 b​​yte [] objData 并尝试设置字节数组的 size

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;

编辑:要从 ImagesStore 中读取数据/ p>

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);
       }
    }
  }
}

您可以使用DataAdapter / DataTable(OP中的代码)

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);
         }
     }

这篇关于如何使用C#在SQL Server中存储和检索DICOM文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆