你指的是使用图片控件显示选择图片,然后保存到数据库吧?读取也是这个意思?
例如在数据库中建立picture表,包含两个字段:BookID,ImgData
保存:
private void button1_Click(object sender, System.EventArgs e)
{
this.openFileDialog1.Filter = "所有文件|*.*|BMP文件|*.bmp";
if(this.openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
this.pictureBox1.Image = new Bitmap(this.openFileDialog1.FileName);
Stream s = this.openFileDialog1.OpenFile(); SqlConnection cn = new SqlConnection();
cn.ConnectionString = "database=xxx;server=localhost;uid=sa;pwd=xxx";
cn.Open(); SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = "INSERT INTO picture VALUES(@BOOKID,@IMAGEDATA)"; SqlParameter p1 = new SqlParameter("@BOOKID", SqlDbType.NVarChar);
p1.Direction = ParameterDirection.Input;
p1.Value= "1";
cmd.Parameters.Add(p1); SqlParameter p2 = new SqlParameter("@IMAGEDATA", SqlDbType.Image);
p2.Direction = ParameterDirection.Input;
p2.Size = Convert.ToInt32(s.Length);
byte[] bs = new byte[s.Length];
s.Read(bs, 0, Convert.ToInt32(s.Length));
p2.Value = bs;
cmd.Parameters.Add(p2); cmd.ExecuteNonQuery();
}
}读取:
private void button2_Click(object sender, System.EventArgs e)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "database=xxx;server=localhost;uid=sa;pwd=xxx";
cn.Open(); SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = "SELECT * FROM picture";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
cn.Close(); byte[] bs = (byte[])ds.Tables[0].Rows[0][1];
MemoryStream ms = new MemoryStream(bs);
pictureBox2.Image = Image.FromStream(ms);
}
例如在数据库中建立picture表,包含两个字段:BookID,ImgData
保存:
private void button1_Click(object sender, System.EventArgs e)
{
this.openFileDialog1.Filter = "所有文件|*.*|BMP文件|*.bmp";
if(this.openFileDialog1.ShowDialog() != DialogResult.Cancel)
{
this.pictureBox1.Image = new Bitmap(this.openFileDialog1.FileName);
Stream s = this.openFileDialog1.OpenFile(); SqlConnection cn = new SqlConnection();
cn.ConnectionString = "database=xxx;server=localhost;uid=sa;pwd=xxx";
cn.Open(); SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = "INSERT INTO picture VALUES(@BOOKID,@IMAGEDATA)"; SqlParameter p1 = new SqlParameter("@BOOKID", SqlDbType.NVarChar);
p1.Direction = ParameterDirection.Input;
p1.Value= "1";
cmd.Parameters.Add(p1); SqlParameter p2 = new SqlParameter("@IMAGEDATA", SqlDbType.Image);
p2.Direction = ParameterDirection.Input;
p2.Size = Convert.ToInt32(s.Length);
byte[] bs = new byte[s.Length];
s.Read(bs, 0, Convert.ToInt32(s.Length));
p2.Value = bs;
cmd.Parameters.Add(p2); cmd.ExecuteNonQuery();
}
}读取:
private void button2_Click(object sender, System.EventArgs e)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "database=xxx;server=localhost;uid=sa;pwd=xxx";
cn.Open(); SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = "SELECT * FROM picture";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
cn.Close(); byte[] bs = (byte[])ds.Tables[0].Rows[0][1];
MemoryStream ms = new MemoryStream(bs);
pictureBox2.Image = Image.FromStream(ms);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货