VS中怎么从数据库读取图片
答案:4 悬赏:70 手机版
解决时间 2021-04-07 23:43
- 提问者网友:缘字诀
- 2021-04-07 06:00
VS中怎么从数据库读取图片
最佳答案
- 五星知识达人网友:躲不过心动
- 2021-04-07 07:12
public DataSet ds = null;
public SqlDataAdapter sda = null;
public static SqlConnection conn = null;
//连接数据库
public void OpenLink()
{
conn = new SqlConnection();
conn.ConnectionString = "Server=192.168.1.5;uid=sa;pwd=555;dataBase=数据库名";
try
{
conn.Open();
}
catch
{
MessageBox.Show("连接数据库丢失!");
}
}
//查询数据表
public void link(String sql)
{
if (conn != null)
{
ds = new DataSet();
sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand(sql, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(sda);
sda.Fill(ds);
}
}
public void Pix(PictureBox px, String name) //显示图片
{
String sql = "Select 识别码 from 下单表 where 订票人='" + name + "'";
byte[] imagebytes = null;
SqlCommand com = new SqlCommand(sql, conn);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr.GetValue(0);
}
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
px.Image = bmpt;
px.SizeMode = PictureBoxSizeMode.StretchImage;
}放工程启动的时候就OpenLink(),把上面代码存储在类文件里面,Pix
()函数就是显示图片。
public SqlDataAdapter sda = null;
public static SqlConnection conn = null;
//连接数据库
public void OpenLink()
{
conn = new SqlConnection();
conn.ConnectionString = "Server=192.168.1.5;uid=sa;pwd=555;dataBase=数据库名";
try
{
conn.Open();
}
catch
{
MessageBox.Show("连接数据库丢失!");
}
}
//查询数据表
public void link(String sql)
{
if (conn != null)
{
ds = new DataSet();
sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand(sql, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(sda);
sda.Fill(ds);
}
}
public void Pix(PictureBox px, String name) //显示图片
{
String sql = "Select 识别码 from 下单表 where 订票人='" + name + "'";
byte[] imagebytes = null;
SqlCommand com = new SqlCommand(sql, conn);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
imagebytes = (byte[])dr.GetValue(0);
}
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
px.Image = bmpt;
px.SizeMode = PictureBoxSizeMode.StretchImage;
}放工程启动的时候就OpenLink(),把上面代码存储在类文件里面,Pix
()函数就是显示图片。
全部回答
- 1楼网友:低音帝王
- 2021-04-07 09:58
从数据库中读取图像文件,并显示在PictureBox控件中
- 2楼网友:舍身薄凉客
- 2021-04-07 08:58
直接取出来再将它附给控件的连接地址属性就可以了!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯