使用ADO.NET实体将图像存储到数据库中 [英] Storing Images into Database using ADO.NET Entity
问题描述
我们如何使用ADO.NET实体存储图像?是否需要使用处理程序来存储?执行回复....:-)
how can we store images using ADO.NET Entity?Is Handler is necessary for storing ?Do reply.... :-)
推荐答案
您好,
您可以借助称为BLOB的Binary大对象来完成此操作,我之前已经在数据库中添加了图像,在这里让我解释一下
声明一个类级别的字符串变量,并使用表单加载事件中的值将其初始化,如下所示:
Hello,
You can do this with the help of what is called as BLOB thats Binary large object, i have added image in database previously here let me expalin you
Declare a class level string variable and initialize it with a value in the forms load event something like this
string str;
private void Form1_Load(object sender, EventArgs e)
{
str = @"D:\Wallpapers\bmw gold\image001.jpg";
}
我已经使用了一个字符串变量,您可以根据需要使用对话框,现在下一步是添加名称空间
在您的应用程序中添加这些名称空间
i have used a string variable you can use dialogboxes if you want, now next step is adding of namespaces
add these namespaces in your application
using System.IO;
using System.Data.SqlClient;
现在单击一个按钮,我已经插入了图像,下面给出了代码
但是在此之前创建一个sql表,就像我也为她创建了该表的代码一样
now on the click of a button i have inserted the image and the code for this is given below
but before that create a sql table just like i did hers the code for that too
create table dummy1
(
ravi image //image is a datatype
);
private void button1_Click(object sender, EventArgs e)
{
if (str.Length != 0)
{
FileStream fs = new FileStream(str, FileMode.OpenOrCreate, FileAccess.Read);
byte[] radix = new byte[fs.Length];
fs.Read(radix, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=radix;Integrated Security=True");
con.Open();
//here dummy1 is the name of my table
SqlCommand cmd = new SqlCommand("insert into dummy1 values(''" + radix + "'')", con);
cmd.ExecuteNonQuery();
MessageBox.Show("Image Inserted");
con.Close();
con.Dispose();
}
上面的代码在执行时会将图像添加到数据库中,并向您显示一个消息框,图像将以二进制格式存储,
一旦找到有用的答案,就对我的答案进行评分
谢谢&问候
基:rose:
The above code when executed will add the image into the database and will display a message box to you and the image will be stored in Binary format,
Do rate my answer once you find it useful
Thanks & Regards
Radix :rose:
这篇关于使用ADO.NET实体将图像存储到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!