现在我想往表里的pic字段里添加一张图片
请朋友们指点一下 该功能将如何实现?
小弟在此谢过了!!!
14 个解决方案
#1
insert yourtable (image)values('d:\a.gif')
#2
insert into Table(image) values('...\xxx.gif')
#3
在pic字段中插入该图片的路径即可,一般为相对路径
#4
http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506
#5
这个是我在我的机器上面作的测试,好像没有什么问题,你看一下.
针对你的实际情况,换成SQL数据库时,连同SqlClient命名空间也换了.
由mdb向SQL转变应该不是很大的问题.
(三个button ,一个文本输入,一个picturebox)
private void Form1_Load(object sender, System.EventArgs e)
{
this.button2.Enabled = false;
this.button2.Text = "上传至库";
this.button1.Text = "上传文件";
this.button3.Text = "根据ID显示图片";
this.strSqlConn = @"Data Source=""F:\C#\ImageToDb\bin\Debug\db.mdb"";Provider=""Microsoft.Jet.OLEDB.4.0"";";
}
private void button2_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != string.Empty )
{
try
{
fs = new FileStream( this.filename,FileMode.Open );
bytes = new byte[(int)fs.Length];
int intFile = fs.Read( bytes,0,bytes.Length);
if (intFile>0)
{
conn = new OleDbConnection( this.strSqlConn );
this.strSqlCmd = "Insert into tab (Img,title,des) values (@ph,@title,@des)" ;
cmd = new OleDbCommand( strSqlCmd,conn );
conn.Open();
cmd.Parameters.Add("@ph",OleDbType.Binary);
cmd.Parameters.Add("@title",OleDbType.VarChar);
cmd.Parameters.Add("@des",OleDbType.LongVarWChar);
cmd.Parameters["@title"].Value = "标题";
cmd.Parameters["@ph"].Value = this.bytes;
cmd.Parameters["@des"].Value = "这个是备注说明字段";
int intS = cmd.ExecuteNonQuery();
if (intS<0)
{
MessageBox.Show("更新失败");
}
MessageBox.Show("成功更新");
}
}
catch(Exception ex)
{
MessageBox.Show( ex.Message );
}
finally
{
conn.Close();
fs.Flush();
fs.Close();
}
}
else
{
MessageBox.Show("请输入准考证号");
}
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
conn = new OleDbConnection( this.strSqlConn);
this.strSqlCmd = "Select img From tab Where id = " + this.textBox1.Text + "";
conn.Open();
cmd = new OleDbCommand( strSqlCmd,conn );
object obj = cmd.ExecuteScalar();
this.bytes = (byte[])obj;
using ( MemoryStream ms = new MemoryStream( bytes,0,bytes.Length ))
{
Bitmap bmp = new Bitmap( ms );
this.pictureBox1.Image = bmp;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"出错了!",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
针对你的实际情况,换成SQL数据库时,连同SqlClient命名空间也换了.
由mdb向SQL转变应该不是很大的问题.
(三个button ,一个文本输入,一个picturebox)
private void Form1_Load(object sender, System.EventArgs e)
{
this.button2.Enabled = false;
this.button2.Text = "上传至库";
this.button1.Text = "上传文件";
this.button3.Text = "根据ID显示图片";
this.strSqlConn = @"Data Source=""F:\C#\ImageToDb\bin\Debug\db.mdb"";Provider=""Microsoft.Jet.OLEDB.4.0"";";
}
private void button2_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != string.Empty )
{
try
{
fs = new FileStream( this.filename,FileMode.Open );
bytes = new byte[(int)fs.Length];
int intFile = fs.Read( bytes,0,bytes.Length);
if (intFile>0)
{
conn = new OleDbConnection( this.strSqlConn );
this.strSqlCmd = "Insert into tab (Img,title,des) values (@ph,@title,@des)" ;
cmd = new OleDbCommand( strSqlCmd,conn );
conn.Open();
cmd.Parameters.Add("@ph",OleDbType.Binary);
cmd.Parameters.Add("@title",OleDbType.VarChar);
cmd.Parameters.Add("@des",OleDbType.LongVarWChar);
cmd.Parameters["@title"].Value = "标题";
cmd.Parameters["@ph"].Value = this.bytes;
cmd.Parameters["@des"].Value = "这个是备注说明字段";
int intS = cmd.ExecuteNonQuery();
if (intS<0)
{
MessageBox.Show("更新失败");
}
MessageBox.Show("成功更新");
}
}
catch(Exception ex)
{
MessageBox.Show( ex.Message );
}
finally
{
conn.Close();
fs.Flush();
fs.Close();
}
}
else
{
MessageBox.Show("请输入准考证号");
}
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
conn = new OleDbConnection( this.strSqlConn);
this.strSqlCmd = "Select img From tab Where id = " + this.textBox1.Text + "";
conn.Open();
cmd = new OleDbCommand( strSqlCmd,conn );
object obj = cmd.ExecuteScalar();
this.bytes = (byte[])obj;
using ( MemoryStream ms = new MemoryStream( bytes,0,bytes.Length ))
{
Bitmap bmp = new Bitmap( ms );
this.pictureBox1.Image = bmp;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"出错了!",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
#6
通过存储过程向数据库插入二进制的字段保存
#7
我想把图片先存到pictureBox.Image里 然后将pictureBox.Image里的值再存到
数据库pic字段里
请问这种方法对不?
insert into information(pic) values('"+pictureBox.Image+"')
请指点一下
谢谢过路的朋友
数据库pic字段里
请问这种方法对不?
insert into information(pic) values('"+pictureBox.Image+"')
请指点一下
谢谢过路的朋友
#8
insert into information(pic) values('"+pictureBox.Image+"')
这样写是不对地,要使用存储过程了
这样写是不对地,要使用存储过程了
#9
如何存储成二进制流呀
能指点一下吗?
能指点一下吗?
#10
数据库中操作图片
How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
把任意类型的文件保存到SQL Server
http://dotnet.aspx.cc/ShowDetail.aspx?id=EY1XLDYV-PIDF-43LO-1WFL-FMY5ALE1F635
#11
如果我的pictureBox.Image里已经存放了二进制流数据
那么该用什么方法能将pictureBox.Image里的流存放到我指定的pic字段里呢
那么该用什么方法能将pictureBox.Image里的流存放到我指定的pic字段里呢
#12
请朋友们帮忙解决一下
谢了
谢了
#13
难道我前面写的代码还解决不了你的问题吗?
#14
//利用选择文件对话框选取图像文件
OpenFileDialog openFileDialog1=new OpenFileDialog();
openFileDialog1.Filter="*.jpg;*.bmp;*.*|*.jpg;*.bmp;*.*";
//如果用户选取了图像文件
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
//产生Stream流对象
Stream myStream = openFileDialog1.OpenFile();
int length=(int)myStream.Length;
//产生字节数组对象
byte[] bytes=new byte[length];
//读取图像文件,将数据放入字节数组中。
myStream.Read(bytes,0,length);
myStream.Close();
上面的方法是将流存到了bytes里,我想请问用什么办法可以把bytes里的数据直接添加到数据库
里的pic字段里
请路过的好心朋友指点一下
小弟在此谢过了!!!!!!!!!!!!!
OpenFileDialog openFileDialog1=new OpenFileDialog();
openFileDialog1.Filter="*.jpg;*.bmp;*.*|*.jpg;*.bmp;*.*";
//如果用户选取了图像文件
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
//产生Stream流对象
Stream myStream = openFileDialog1.OpenFile();
int length=(int)myStream.Length;
//产生字节数组对象
byte[] bytes=new byte[length];
//读取图像文件,将数据放入字节数组中。
myStream.Read(bytes,0,length);
myStream.Close();
上面的方法是将流存到了bytes里,我想请问用什么办法可以把bytes里的数据直接添加到数据库
里的pic字段里
请路过的好心朋友指点一下
小弟在此谢过了!!!!!!!!!!!!!
#1
insert yourtable (image)values('d:\a.gif')
#2
insert into Table(image) values('...\xxx.gif')
#3
在pic字段中插入该图片的路径即可,一般为相对路径
#4
http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506
#5
这个是我在我的机器上面作的测试,好像没有什么问题,你看一下.
针对你的实际情况,换成SQL数据库时,连同SqlClient命名空间也换了.
由mdb向SQL转变应该不是很大的问题.
(三个button ,一个文本输入,一个picturebox)
private void Form1_Load(object sender, System.EventArgs e)
{
this.button2.Enabled = false;
this.button2.Text = "上传至库";
this.button1.Text = "上传文件";
this.button3.Text = "根据ID显示图片";
this.strSqlConn = @"Data Source=""F:\C#\ImageToDb\bin\Debug\db.mdb"";Provider=""Microsoft.Jet.OLEDB.4.0"";";
}
private void button2_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != string.Empty )
{
try
{
fs = new FileStream( this.filename,FileMode.Open );
bytes = new byte[(int)fs.Length];
int intFile = fs.Read( bytes,0,bytes.Length);
if (intFile>0)
{
conn = new OleDbConnection( this.strSqlConn );
this.strSqlCmd = "Insert into tab (Img,title,des) values (@ph,@title,@des)" ;
cmd = new OleDbCommand( strSqlCmd,conn );
conn.Open();
cmd.Parameters.Add("@ph",OleDbType.Binary);
cmd.Parameters.Add("@title",OleDbType.VarChar);
cmd.Parameters.Add("@des",OleDbType.LongVarWChar);
cmd.Parameters["@title"].Value = "标题";
cmd.Parameters["@ph"].Value = this.bytes;
cmd.Parameters["@des"].Value = "这个是备注说明字段";
int intS = cmd.ExecuteNonQuery();
if (intS<0)
{
MessageBox.Show("更新失败");
}
MessageBox.Show("成功更新");
}
}
catch(Exception ex)
{
MessageBox.Show( ex.Message );
}
finally
{
conn.Close();
fs.Flush();
fs.Close();
}
}
else
{
MessageBox.Show("请输入准考证号");
}
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
conn = new OleDbConnection( this.strSqlConn);
this.strSqlCmd = "Select img From tab Where id = " + this.textBox1.Text + "";
conn.Open();
cmd = new OleDbCommand( strSqlCmd,conn );
object obj = cmd.ExecuteScalar();
this.bytes = (byte[])obj;
using ( MemoryStream ms = new MemoryStream( bytes,0,bytes.Length ))
{
Bitmap bmp = new Bitmap( ms );
this.pictureBox1.Image = bmp;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"出错了!",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
针对你的实际情况,换成SQL数据库时,连同SqlClient命名空间也换了.
由mdb向SQL转变应该不是很大的问题.
(三个button ,一个文本输入,一个picturebox)
private void Form1_Load(object sender, System.EventArgs e)
{
this.button2.Enabled = false;
this.button2.Text = "上传至库";
this.button1.Text = "上传文件";
this.button3.Text = "根据ID显示图片";
this.strSqlConn = @"Data Source=""F:\C#\ImageToDb\bin\Debug\db.mdb"";Provider=""Microsoft.Jet.OLEDB.4.0"";";
}
private void button2_Click(object sender, System.EventArgs e)
{
if (this.textBox1.Text != string.Empty )
{
try
{
fs = new FileStream( this.filename,FileMode.Open );
bytes = new byte[(int)fs.Length];
int intFile = fs.Read( bytes,0,bytes.Length);
if (intFile>0)
{
conn = new OleDbConnection( this.strSqlConn );
this.strSqlCmd = "Insert into tab (Img,title,des) values (@ph,@title,@des)" ;
cmd = new OleDbCommand( strSqlCmd,conn );
conn.Open();
cmd.Parameters.Add("@ph",OleDbType.Binary);
cmd.Parameters.Add("@title",OleDbType.VarChar);
cmd.Parameters.Add("@des",OleDbType.LongVarWChar);
cmd.Parameters["@title"].Value = "标题";
cmd.Parameters["@ph"].Value = this.bytes;
cmd.Parameters["@des"].Value = "这个是备注说明字段";
int intS = cmd.ExecuteNonQuery();
if (intS<0)
{
MessageBox.Show("更新失败");
}
MessageBox.Show("成功更新");
}
}
catch(Exception ex)
{
MessageBox.Show( ex.Message );
}
finally
{
conn.Close();
fs.Flush();
fs.Close();
}
}
else
{
MessageBox.Show("请输入准考证号");
}
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
conn = new OleDbConnection( this.strSqlConn);
this.strSqlCmd = "Select img From tab Where id = " + this.textBox1.Text + "";
conn.Open();
cmd = new OleDbCommand( strSqlCmd,conn );
object obj = cmd.ExecuteScalar();
this.bytes = (byte[])obj;
using ( MemoryStream ms = new MemoryStream( bytes,0,bytes.Length ))
{
Bitmap bmp = new Bitmap( ms );
this.pictureBox1.Image = bmp;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"出错了!",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
conn.Close();
}
}
#6
通过存储过程向数据库插入二进制的字段保存
#7
我想把图片先存到pictureBox.Image里 然后将pictureBox.Image里的值再存到
数据库pic字段里
请问这种方法对不?
insert into information(pic) values('"+pictureBox.Image+"')
请指点一下
谢谢过路的朋友
数据库pic字段里
请问这种方法对不?
insert into information(pic) values('"+pictureBox.Image+"')
请指点一下
谢谢过路的朋友
#8
insert into information(pic) values('"+pictureBox.Image+"')
这样写是不对地,要使用存储过程了
这样写是不对地,要使用存储过程了
#9
如何存储成二进制流呀
能指点一下吗?
能指点一下吗?
#10
数据库中操作图片
How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
把任意类型的文件保存到SQL Server
http://dotnet.aspx.cc/ShowDetail.aspx?id=EY1XLDYV-PIDF-43LO-1WFL-FMY5ALE1F635
#11
如果我的pictureBox.Image里已经存放了二进制流数据
那么该用什么方法能将pictureBox.Image里的流存放到我指定的pic字段里呢
那么该用什么方法能将pictureBox.Image里的流存放到我指定的pic字段里呢
#12
请朋友们帮忙解决一下
谢了
谢了
#13
难道我前面写的代码还解决不了你的问题吗?
#14
//利用选择文件对话框选取图像文件
OpenFileDialog openFileDialog1=new OpenFileDialog();
openFileDialog1.Filter="*.jpg;*.bmp;*.*|*.jpg;*.bmp;*.*";
//如果用户选取了图像文件
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
//产生Stream流对象
Stream myStream = openFileDialog1.OpenFile();
int length=(int)myStream.Length;
//产生字节数组对象
byte[] bytes=new byte[length];
//读取图像文件,将数据放入字节数组中。
myStream.Read(bytes,0,length);
myStream.Close();
上面的方法是将流存到了bytes里,我想请问用什么办法可以把bytes里的数据直接添加到数据库
里的pic字段里
请路过的好心朋友指点一下
小弟在此谢过了!!!!!!!!!!!!!
OpenFileDialog openFileDialog1=new OpenFileDialog();
openFileDialog1.Filter="*.jpg;*.bmp;*.*|*.jpg;*.bmp;*.*";
//如果用户选取了图像文件
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
//产生Stream流对象
Stream myStream = openFileDialog1.OpenFile();
int length=(int)myStream.Length;
//产生字节数组对象
byte[] bytes=new byte[length];
//读取图像文件,将数据放入字节数组中。
myStream.Read(bytes,0,length);
myStream.Close();
上面的方法是将流存到了bytes里,我想请问用什么办法可以把bytes里的数据直接添加到数据库
里的pic字段里
请路过的好心朋友指点一下
小弟在此谢过了!!!!!!!!!!!!!