且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在SQL Server数据库中存储图像

更新时间:2023-02-07 10:19:52

阅读以下内容: ^ ]
Read the following : Storing and Retrieving Images from SQL Server Using Strored Procedures and C#.net[^]


google停止工作了吗?

尝试在 google [ CodeProject文章 [ ^ ]或 CodeProject质量检查 [ ^ ]

这是简单的代码段,使用FileUpload控件将图像存储在SQL中.
Does google stops working?

Try search on google[^] first.

You can also try search on CodeProject Articles[^] or CodeProject QA[^]

This is simple code snippets which stores image in SQL using FileUpload control
FileStream file = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
            BinaryReader str1 = new BinaryReader(file);
            byte[] bit = str1.ReadBytes(Convert.ToInt32( file.Length));
            SqlConnection connection = new SqlConnection("server=(local);database=test;integrated security=true");
            connection.Open();
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandType = CommandType.Text;
            command.Parameters.AddWithValue("@Photo",bit);
            command.CommandText = "INSERT INTO image1(pic) VALUES (@Photo)";
            command.ExecuteNonQuery();
            connection.Close();


您要将图像文件存储在数据库中,请转到以下位置:-
首先获取FileUpload控件,并提供该控件的ID,例如fileUpload.
现在在button_Click上单击一个按钮,编写以下代码:-

You want to store your image file in database o.k here you go:-
first take a FileUpload Control provide the Id of this control say fileUpload.
now take a button on button_Click write the following code:-

int length = fileUpload.PostedFile.ContentLength;
byte[] imgByte = new byte[length];
HttpPostedFile hpf = fileUpload.PostedFile;
hpf.InputStream.Read(imgByte, 0, length);



现在将imgByte对象插入数据库.
记住您的数据库列的数据类型必须为Image类型的图像.



Now insert imgByte object into your database.
Remember datatype of your database column for Image must of type Image.