繁体   English   中英

将图像(二进制格式)从数据库存储到文件系统

[英]Store Images(in binary format) From Database to FileSystem

伙计们! 我有一个任务是从位于类别表中的罗斯文数据库中检索所有图像(它们以二进制格式给出),并将它们以JPG格式存储在文件系统中。 我真的很陌生,需要一些帮助。 我想出了这一点,但根本没有用。

private static void WriteBinaryData(string fileName, byte[] fileContents)
    {
        FileStream stream = File.OpenWrite(fileName);

        using (stream)
        {
            stream.Write(fileContents, 0, fileContents.Length);
        }
    }


    static void Main(string[] args)
    {

        SqlConnection conn = new SqlConnection("Server = HOME-PC\\KRISKO; " +
        "Database=Northwind; Integrated Security=true");
        conn.Open();

        using (conn)
        {
            SqlCommand cmd = new SqlCommand("SELECT Picture FROM Categories", conn);
            SqlDataReader reader = cmd.ExecuteReader();

            using (reader)
            {
                while (reader.Read())
                {
                    byte[] image = (byte[])reader["Picture"];
                    WriteBinaryData("C:\\Users\\Niki\\Desktop", image);
                }
            }
        }
    }

我在这里看到以下几个问题

  1. 您到文件C:\\\\Users\\\\Niki\\\\Desktop的路径可能是一个问题,因为如果这是另一个用户帐户配置文件,则您可能无法访问它; 不是你的。

  2. 您没有为图片文件提供文件名,如以下代码所示: FileStream stream = File.OpenWrite(fileName); 这里的fileName变量只有路径,没有与之关联的文件名

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM