string ConnectionString ="server=.;uid=sa;pwd=;database=system";
SqlConnection conn=new SqlConnection(ConnectionString);
string sql="select * from table1";
SqlCommand command = new SqlCommand(sql,conn);
conn.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
byte[] image =(byte[])dr[0];
MemoryStream ms = new MemoryStream(image);
this.pictureBox1.Image = Image.FromStream(ms);
}
else
{
MessageBox.Show("没有成功读入数据!") ;
}
SqlConnection conn=new SqlConnection(ConnectionString);
string sql="select * from table1";
SqlCommand command = new SqlCommand(sql,conn);
conn.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
byte[] image =(byte[])dr[0];
MemoryStream ms = new MemoryStream(image);
this.pictureBox1.Image = Image.FromStream(ms);
}
else
{
MessageBox.Show("没有成功读入数据!") ;
}
加了 using System.IO
就是报 “使用了无效参数。”
MemoryStream myStream = new MemoryStream(image, true);
myStream.Write(image, 0, image.Length);
this.pictureBox1.Image = Image.FromStream(myStream);也不确定..
(
aa image,
bb varchar(200)
)你看是不是第一个呀。。这个问题好久还没解决呀。。哭呀
this.pictureBox1.Image = Image.FromStream(ms);前一句还是后一句啊?到底是哪个参数无效?
我存的时候调用 byte[] photo = GetPhoto(this.openFileDialog1.FileName); 这个函数啊。。帮我看看呀有没有错呀
private void button2_Click(object sender, System.EventArgs e)
{
//this.pictureBox1.Image=null;
try
{
byte[] photo = GetPhoto(this.openFileDialog1.FileName);
SqlConnection nwindConn = new SqlConnection("server=.;database=System;uid=sa;pwd=;"); SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values('"+photo+"')", nwindConn); nwindConn.Open(); addEmp.ExecuteNonQuery(); nwindConn.Close();
}
catch(SqlException x)
{
MessageBox.Show(x.Message);
} } public static byte[] GetPhoto(string filePath)
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
return photo;
}
private void 新增学生表_Load(object sender, System.EventArgs e)
{
} private void button1_Click(object sender, System.EventArgs e) //浏览要添加的图片
{
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
try
{
this.pictureBox1.Image=Image.FromFile(this.openFileDialog1.FileName);
}
catch(Exception x)
{
MessageBox.Show("图片的像素过大"+x.Message);
}
}
} private void button2_Click(object sender, System.EventArgs e) //存储SQL图片
{
//this.pictureBox1.Image=null;
try
{
byte[] photo = GetPhoto(this.openFileDialog1.FileName);
SqlConnection nwindConn = new SqlConnection("server=.;database=System;uid=sa;pwd=;"); SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values('"+photo+"')", nwindConn); nwindConn.Open(); addEmp.ExecuteNonQuery(); nwindConn.Close();
}
catch(SqlException x)
{
MessageBox.Show(x.Message);
} } public static byte[] GetPhoto(string filePath) //函数
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
return photo;
} private void button3_Click(object sender, System.EventArgs e) //读取 SQL图片
{
try
{
string ConnectionString ="server=.;uid=sa;pwd=;database=system";
SqlConnection conn=new SqlConnection(ConnectionString);
string sql="select aa from table1 where bb='keling'";
SqlCommand command = new SqlCommand(sql,conn);
conn.Open();
SqlDataReader dr = command.ExecuteReader(CommandBehavior.SequentialAccess);
if(dr.Read())
{
//byte[] image =(byte[])dr[0];
//MemoryStream ms = new MemoryStream(((byte[])dr["aa"]));
//this.pictureBox1.Image = Image.FromStream(ms,true);
byte[] image =(byte[])dr["aa"];
MemoryStream myStream = new MemoryStream(image, true);
myStream.Write(image, 0, image.Length);
this.pictureBox1.Image = Image.FromStream(myStream);
}
else
{
MessageBox.Show("没有成功读入数据!") ;
}
conn.Close();
}
catch(Exception x)
{
MessageBox.Show(x.Message);
}
}
addEmp.Parameters.Add("@Image", pthoto);
byte[] photo = GetPhoto(this.textBox1.Text);
SqlConnection nwindConn = new SqlConnection("server=.;database=test;uid=sa;pwd=0421"); SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values(@Image)", nwindConn);
addEmp.Parameters.AddWithValue("@Image", photo); nwindConn.Open(); addEmp.ExecuteNonQuery();......我的是Vs2005,所以用的AddWithValue,你2003可以用Add方法...试试看吧...
SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values(@Image)", nwindConn);
addEmp.Parameters.AddWithValue("@Image", photo);
这个呀
SqlCommand addEmp = new SqlCommand("INSERT INTO table1(aa) Values(@Image)", nwindConn);
addEmp.Parameters.AddWithValue("@Image", photo);至少不能像你那样用,因为它是字节数组,所以不能用单引号引起来...
如果我用 INSERT INTO table1(aa) Values(""+photo+"")。这样可以吗
while (sqlDataReader.Read())
{
//这里 改这个地方( byte[] image =(byte[])dr[0]; )
MemoryStream memoryStream=new MemoryStream(sqlDataReader.GetSqlBinary(1).Value);Image listViewImage=Image.FromStream (memoryStream);
imageList.Images.Add(listViewImage);
}
this.listView1.SmallImageList=imageList;