Byte[] blob = null; FileStream fs = null; const string sConn = "server=(local);Initial Catalog=Northwind;UID=ctester;PWD=password"; try { SqlConnection conn = new SqlConnection(sConn); SqlCommand cmd = new SqlCommand("SELECT Picture FROM Categories WHERE CategoryName='Builder'", conn); cn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
blob = new Byte[(sdr.GetBytes(0, 0, null, 0, int.MaxValue))];
sdr.GetBytes[0, 0, blob, 0, blob.Length);
sdr.Close(); conn.Close();
fs = new FileStream("c://Builder.doc", FileMode.Create, FileAccess.Write);
fs.Write(blob, 0, blob.Length);
fs.Close(); }
catch (SqlException e)
{ Console.WriteLine("SQL Exception: " + e.Message); }
catch (Exception e) { Console.WriteLine("Exception: "+ e.Message); }
本文详细介绍了如何使用SQL查询从数据库中获取图片数据,并将其保存为本地文件的过程。通过实例演示了如何连接数据库、执行查询、读取图片数据以及将图片数据写入本地文件的方法。
467

被折叠的 条评论
为什么被折叠?



