CREATE DATABASE ImageTest GO USE ImageTest; GO CREATE TABLE ImageTable ( ImageData varBinary(MAX) );
INSERT INTO ImageTable (ImageData) SELECT * FROM OPENROWSET(BULK N‘C:\aa.jpg‘, SINGLE_BLOB) AS Photo GO
EXEC sp_configure ‘show advanced options‘, 1 GO RECONFIGURE GO EXEC sp_configure ‘xp_cmdshell‘, 1 GO RECONFIGURE GO
EXEC master..xp_cmdshell ‘bcp "SELECT ImageData FROM ImageTest.dbo.ImageTable" queryout "c:\bb.jpg"-T -c‘ Go
public static bool StoreImages(string[] fileNames, string[] filePaths) { try { for (int i = 0; i < fileNames.Length; i++) { string fileName = fileNames[i]; string filePath = filePaths[i]; using (SqlConnection connection = new SqlConnection(PubVariant.ConnectionString)) { connection.Open(); FileStream byteStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); byte[] byteImage = new byte[byteStream.Length]; byteStream.Read(byteImage, 0, (int)byteStream.Length); string strSql = "insert into Data(Name,Data,DataPath) values(@Name,@Data,@DataPath)"; using (SqlCommand cmd = new SqlCommand(strSql, connection)) { cmd.Parameters.Add("@Name", SqlDbType.Text); cmd.Parameters.Add("@Data", SqlDbType.Binary); cmd.Parameters.Add("@DataPath", SqlDbType.Text); cmd.Parameters["@Name"].Value = fileName; cmd.Parameters["@Data"].Value = byteImage; cmd.Parameters["@DataPath"].Value = filePath; cmd.ExecuteNonQuery(); } } } return true; } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } }
public static Image GetImageFromName(string fileName) { try { using (SqlConnection connection = new SqlConnection(PubVariant.ConnectionString)) { connection.Open(); string strSql = "select Data from Data where Name = ‘" + fileName + "‘"; SqlCommand cmd = new SqlCommand(strSql, connection); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); MemoryStream ms = new MemoryStream((byte[])dr[0]); Image img = Image.FromStream(ms); return img; } } catch (Exception ex) { MessageBox.Show(ex.Message); return null; } }
SQL SERVER 2008中使用VARBINARY(MAX)进行图像存取的实现方法,布布扣,bubuko.com
SQL SERVER 2008中使用VARBINARY(MAX)进行图像存取的实现方法
原文:http://www.cnblogs.com/qxzy/p/3835483.html