【问题标题】:How i can store image in database using C#?我如何使用 C# 将图像存储在数据库中?
【发布时间】:2017-06-27 00:11:12
【问题描述】:

这是我在 Grid(WPF) 中打印图像的 C# 代码,现在我想 将此图像存储在数据库中,我在数据库中有名为的列 图片。我有所有其他代码数据库连接等。请告诉我哪个 方法最适合在数据库中存储图像?

 private void button_Browse_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog op = new OpenFileDialog();
        op.Title = "Select a picture";
        op.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
          "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
          "Portable Network Graphic (*.png)|*.png";
        if (op.ShowDialog() == true)
        {
        imgPhoto.Source = new BitmapImage(new Uri(op.FileName));//this line print image in Grid

        }
    }

【问题讨论】:

  • 什么数据库?这在很大程度上取决于您正在谈论的具体数据库系统......
  • @marc_s SQl Server Database at column datatype is image.
  • ntexttextimage 数据类型将在 SQL Server 的未来版本中删除。避免在新的开发工作中使用这些数据类型,并计划修改当前使用它们的应用程序。请改用nvarchar(max)varchar(max)varbinary(max)See details here
  • @marc_s 如果我使用 varchar(max) 或 carbinary(max) 那么我将如何将图像类型转换为 varchar(max) 或其他
  • 对于 二进制 数据(如图像),使用 varbinary(max) - 您应该能够简单地将列的数据类型转换为 ALTER TABLE dbo.YourTable ALTER COLUMN YourColumn VARBINARY(MAX);

标签: sql-server wpf database image-processing c#-4.0


【解决方案1】:

您可以使用VARBINARY(MAX) 存储任何类型的文件:

我自己给你举个例子,希望对你有帮助。

首先创建一个表(比如 tblSampleImage)然后添加两列(FileName VARCHAR(20) , FileContent VARBINARY(MAX)

以下是从数据库中插入和获取文件的示例 CS 代码:

public void insertFile()
{
   string fileName= Path.GetFileName(@"your file full path");
   string filePath= Path.GetFullPath(@"your file full path");
   if (!File.Exists(filePath))
            {
                MessageBox.Show("File not found");
                return;
            }
    byte[] contents= File.ReadAllBytes(filePath);
    string insertStmt = "INSERT INTO tblSampleImage(FileName, FileContent) VALUES(@FileName, @FileContent)";
    SqlConnection connection = new SqlConnection();
    connection.ConnectionString = "connectionString";
    using (SqlCommand cmdInsert = new SqlCommand(insertStmt, connection))
          {
          cmdInsert.Parameters.Add("@FileName", SqlDbType.VarChar, 20).Value = fileName;
          cmdInsert.Parameters.Add("@FileContent", SqlDbType.VarBinary, int.MaxValue).Value = contents;
          connection.Open();
          cmdInsert.ExecuteNonQuery();
           connection.Close();
          }   
}

获取文件

   public void fetchFile()
        {
        string newFilePath = Path.GetFullPath(@"your new path where you store your fetched file");
        string fileName="File1"; //this is the file's name which is stored in database and you want to fetch
         byte[] fileContents;
         string selectStmt = "SELECT FileContent FROM tblSampleImage WHERE FileName = @FileName";
         SqlConnection connection = new SqlConnection();
         connection.ConnectionString = "connectionString";
         using (SqlCommand cmdSelect = new SqlCommand(selectStmt, connection))
            {
               cmdSelect.Parameters.Add("@Filename", SqlDbType.VarChar).Value = fileName;
               connection.Open();
               fileContents = (byte[])cmdSelect.ExecuteScalar();
               connection.Close();
            }
         File.WriteAllBytes(newFilePath, fileContents);
         MessageBox.Show("File Saved at:  " + newFilePath);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2012-03-10
    • 2012-06-08
    • 1970-01-01
    相关资源
    最近更新 更多