高手指教:怎样在access下写进图片和读出图片的 急!!!!!急!!!!急!!!谢谢指教!! 当blob字段插进去就行了。用BinaryReader,Stream就可以了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 转:-------把图片读到pictureBox,再写入数据库SqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");conn.Open();SqlCommand cmd=new SqlCommand("insert into fuser values ('1a','1b',@i)",conn);byte[] ib=new Byte[60000];FileStream fs=new FileStream(@"D:\windows temp\temp\1.jpg",FileMode.Open ,FileAccess.Read );fs.Read(ib,0,60000);cmd.Parameters.Add("@i",SqlDbType.Image,(int)fs.Length);cmd.Parameters["@i"].Value=ib;cmd.ExecuteNonQuery();conn.Close();-------------------------------------------------------从数据库读图片到pictureboxSqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");conn.Open();SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);SqlDataReader reader=cmd.ExecuteReader();reader.Read();MemoryStream buf=new MemoryStream((byte[])reader[0]);Image image=Image.FromStream(buf,true);pictureBox1.Image=image; //插入DataRowCollection rc; DataRow myNewRow;// Create an array with three elements.object[] rowVals = new object[11];rc = DS.Tables[0].Rows;rowVals[0] = EmployeeID.Text;rowVals[1] = FirstName.Text;rowVals[2] = LastName.Text;rowVals[3] = Title.Text;rowVals[4] = Extension.Text;rowVals[5] = Mobilephone.Text;rowVals[6] = officelocation.Text;rowVals[7] = System.DateTime.Parse(Birthday.Text);rowVals[8] = sittinglocation.Text;rowVals[10] = comboBox1.SelectedValue;if (openFileDialog1.FileName!=null && openFileDialog1.FileName!=""){ FileStream st = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read); BinaryReader mbr = new BinaryReader(st); Byte[] buffer= new byte[st.Length]; mbr.Read(buffer, 0, (int)(st.Length)); st.Close(); rowVals[9] = buffer;}else{ rowVals[9] = DBNull.Value;//OleDbType.Empty;}// Add and return the new row.try{ myNewRow = rc.Add(rowVals); oleDbDataAdapter1.Update(DS.Tables[0]); DS.AcceptChanges(); CurrentRow=DS.Tables[0].Rows.Count-1;}catch (Exception ex){ MessageBox.Show(ex.ToString());}//图像选择 private void picture_DoubleClick(object sender, System.EventArgs e) { openFileDialog1.InitialDirectory = @"C:\"; openFileDialog1.DefaultExt = "jpg"; openFileDialog1.Filter = "Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg"; openFileDialog1.ShowDialog(); picture.Image = Image.FromFile(openFileDialog1.FileName); }//显示图像 private void Display_Record(int CurrentRow) { DS.Tables[0].Clear(); oleDbDataAdapter1.Fill(DS,"employee"); DataView dv_employee = new DataView(DS.Tables[0],"employeeid='"+DS.Tables[0].Rows[CurrentRow][0].ToString()+"'","",DataViewRowState.CurrentRows); try { if (CurrentRow>=0 && CurrentRow<=DS.Tables[0].Rows.Count -1) { EmployeeID.Text=dv_employee.Table.Rows[CurrentRow][0].ToString(); FirstName.Text=dv_employee.Table.Rows[CurrentRow][1].ToString(); LastName.Text=dv_employee.Table.Rows[CurrentRow][2].ToString(); Title.Text=dv_employee.Table.Rows[CurrentRow][3].ToString(); Extension.Text=dv_employee.Table.Rows[CurrentRow][4].ToString(); Mobilephone.Text=dv_employee.Table.Rows[CurrentRow][5].ToString(); officelocation.Text=dv_employee.Table.Rows[CurrentRow][6].ToString(); Birthday.Text=Convert.ToString(dv_employee.Table.Rows[CurrentRow][7].ToString()); sittinglocation.Text=dv_employee.Table.Rows[CurrentRow][8].ToString(); if (dv_employee.Table.Rows[CurrentRow][9] != null && dv_employee.Table.Rows[CurrentRow][9] != DBNull.Value) { byte[] p_stream = (byte[])dv_employee.Table.Rows[CurrentRow][9]; Stream st = new MemoryStream(p_stream); picture.Image=Image.FromStream(st); st.Close(); } else { picture.Image=null; } picture.Refresh(); comboBox1.SelectedValue=dv_employee.Table.Rows[CurrentRow][10].ToString(); RecordNumber.Text="CurrentRow is " + Convert.ToString(CurrentRow+1) + ", Total is "+DS.Tables[0].Rows.Count.ToString(); } else { RecordNumber.Text="No Record!"; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } xiangding(谁知盘中餐,离离皆辛苦(同志们啊,要珍惜现在的幸福) BearRui(我有点笨,但我很特别,所以我特别笨!) wangsaokui(无间道III(终极无间)) ( 楼上三位再加我都要给分 楼主知道吗? flash 和c#之间的传值,问题,希望高手进来帮忙 举例求递归算法 Exception 的问题 这个正则帮我看看那里错了。。。 C# windows 应用程序中怎么根据给定地址显示图片? 如何当鼠标移到一个图标时,显示相应的信息,求指教 @@@@@@@思归老大请帮忙 ,c#局域网中在另一台PC中建立目录???? 等^^ 添加新行更新数据后,怎么获取ID字段值 各位大哥看看我的代码哪里错了!!谢谢!! Winform有关于Windows media player的问题 VS2003里radiobutton控件怎么控制只能单选阿? 这个小程序如何写?
-------
把图片读到pictureBox,再写入数据库SqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("insert into fuser values ('1a','1b',@i)",conn);
byte[] ib=new Byte[60000];
FileStream fs=new FileStream(@"D:\windows temp\temp\1.jpg",FileMode.Open ,FileAccess.Read );
fs.Read(ib,0,60000);
cmd.Parameters.Add("@i",SqlDbType.Image,(int)fs.Length);
cmd.Parameters["@i"].Value=ib;
cmd.ExecuteNonQuery();
conn.Close();-------------------------------------------------------
从数据库读图片到pictureboxSqlConnection conn=new SqlConnection(@"data source=chenyuming2004\VSdotNET;uid=sa;pwd=cym;database=lhf");
conn.Open();
SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
MemoryStream buf=new MemoryStream((byte[])reader[0]);
Image image=Image.FromStream(buf,true);
pictureBox1.Image=image;
DataRowCollection rc;
DataRow myNewRow;
// Create an array with three elements.
object[] rowVals = new object[11];
rc = DS.Tables[0].Rows;
rowVals[0] = EmployeeID.Text;
rowVals[1] = FirstName.Text;
rowVals[2] = LastName.Text;
rowVals[3] = Title.Text;
rowVals[4] = Extension.Text;
rowVals[5] = Mobilephone.Text;
rowVals[6] = officelocation.Text;
rowVals[7] = System.DateTime.Parse(Birthday.Text);
rowVals[8] = sittinglocation.Text;
rowVals[10] = comboBox1.SelectedValue;if (openFileDialog1.FileName!=null && openFileDialog1.FileName!="")
{
FileStream st = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
BinaryReader mbr = new BinaryReader(st);
Byte[] buffer= new byte[st.Length];
mbr.Read(buffer, 0, (int)(st.Length));
st.Close();
rowVals[9] = buffer;
}
else
{
rowVals[9] = DBNull.Value;//OleDbType.Empty;
}
// Add and return the new row.
try
{
myNewRow = rc.Add(rowVals); oleDbDataAdapter1.Update(DS.Tables[0]);
DS.AcceptChanges();
CurrentRow=DS.Tables[0].Rows.Count-1;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
//图像选择
private void picture_DoubleClick(object sender, System.EventArgs e)
{
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.DefaultExt = "jpg";
openFileDialog1.Filter = "Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg";
openFileDialog1.ShowDialog();
picture.Image = Image.FromFile(openFileDialog1.FileName);
}//显示图像
private void Display_Record(int CurrentRow)
{
DS.Tables[0].Clear();
oleDbDataAdapter1.Fill(DS,"employee");
DataView dv_employee = new DataView(DS.Tables[0],"employeeid='"+DS.Tables[0].Rows[CurrentRow][0].ToString()+"'","",DataViewRowState.CurrentRows);
try
{
if (CurrentRow>=0 && CurrentRow<=DS.Tables[0].Rows.Count -1)
{
EmployeeID.Text=dv_employee.Table.Rows[CurrentRow][0].ToString();
FirstName.Text=dv_employee.Table.Rows[CurrentRow][1].ToString();
LastName.Text=dv_employee.Table.Rows[CurrentRow][2].ToString();
Title.Text=dv_employee.Table.Rows[CurrentRow][3].ToString();
Extension.Text=dv_employee.Table.Rows[CurrentRow][4].ToString();
Mobilephone.Text=dv_employee.Table.Rows[CurrentRow][5].ToString();
officelocation.Text=dv_employee.Table.Rows[CurrentRow][6].ToString();
Birthday.Text=Convert.ToString(dv_employee.Table.Rows[CurrentRow][7].ToString());
sittinglocation.Text=dv_employee.Table.Rows[CurrentRow][8].ToString(); if (dv_employee.Table.Rows[CurrentRow][9] != null && dv_employee.Table.Rows[CurrentRow][9] != DBNull.Value)
{
byte[] p_stream = (byte[])dv_employee.Table.Rows[CurrentRow][9];
Stream st = new MemoryStream(p_stream);
picture.Image=Image.FromStream(st);
st.Close();
}
else
{
picture.Image=null;
}
picture.Refresh(); comboBox1.SelectedValue=dv_employee.Table.Rows[CurrentRow][10].ToString();
RecordNumber.Text="CurrentRow is " + Convert.ToString(CurrentRow+1) + ", Total is "+DS.Tables[0].Rows.Count.ToString();
}
else
{
RecordNumber.Text="No Record!";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
xiangding(谁知盘中餐,离离皆辛苦(同志们啊,要珍惜现在的幸福)
BearRui(我有点笨,但我很特别,所以我特别笨!) wangsaokui(无间道III(终极无间)) (
楼上三位再加我都要给分 楼主知道吗?