RT
解决方案 »
- 用c#和sql server做个通讯录
- 这样的“List<>集合”应该怎样绑定
- C#机试题
- gridview控件的标题文本?
- 如何通过Text控件,对网格数据进行编辑,然后再通过UPDATE更新数据库库
- 如何在OpenFileDialog控件中同时显示多种格式的图片文件
- 帮忙看一下这个文件是那种编码格式生成的啊?如何重新生.TXT文件
- 如何在WinCE 5.0设备应用程序中访问远程服务器?
- 求c#实现概率问题?通分、约分……
- C#正则表达式如何匹配多行连着<div>?
- 我的黑白棋程序2.0版发布了(棋力已经大增!对AI或黑白棋感兴趣的进来)
- [C# Winform]如何使用progressbar来显示检索数据的进度
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(); SqlConnection cn = new SqlConnection(db.getIP());
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.Parameters.Add("@Picture", SqlDbType.Image, photo.Length).Value = photo;
cmd.Parameters.Add("@burea", SqlDbType.Char).Value = bb.Text.Trim (); cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
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;
public void imgstore(HtmlInputFile UP_FILE,string strsql,string ImageData,string ImageContentType,string ImageSize,Label txtMessage)
{
Int32 FileLength = 0;
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength; //记录文件长度
try
{
if (FileLength != 0)
{
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
objstream= UpFile.InputStream; //建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
objstream.Read(FileByteArray,0,FileLength);
objcomm = new SqlCommand(strsql,GetCN());
objcomm.Parameters.Add(ImageData,SqlDbType.Binary, FileLength).Value = FileByteArray;
objcomm.Parameters.Add(ImageContentType, SqlDbType.VarChar,50).Value = UpFile.ContentType; //记录文件类型
//记录文件长度,读取时使用
objcomm.Parameters.Add(ImageSize, SqlDbType.BigInt,8).Value = UpFile.ContentLength;
objcomm.ExecuteNonQuery();
CloseCN();
txtMessage.Text = "<b>OK!你已经成功上传你的图片</b>";//提示上传成功
}
}
catch (Exception ex)
{
txtMessage.Text = ex.Message.ToString();
}
}
#endregion #region 从数据库中提取图片信息以显示
public void ShowPhoto(string strId,string tblname,string ImageData,string ImageContentType,string ImageSize)
{
string strsql = "select "+ImageData+","+ImageContentType+","+ImageSize+" from "+tblname+" where id='"+strId+"'";
objdr = GetReader(strsql);
if(objdr.Read() && objdr[ImageSize].ToString()!="")
{
HttpContext.Current.Response.ContentType = objdr[ImageContentType].ToString();//设定输出文件类型
//输出图象文件二进制数制
HttpContext.Current.Response.BinaryWrite( (byte[]) objdr[ImageData] );
}
CloseCN();
}
#endregion
从数据库读出数据在网页上显示这副图我还是没搞懂我想在ImageButton上显示这副图正解在哪里?
不过是二进制
1L,3L ,4L
写用二进制存
读用二进制读
据库结构
create table test
{
id int identity(1,1),
FImage image
}
相关的存储过程
Create proc UpdateImage (@UpdateImage Image)
As
Insert Into test (FImage) values (@UpdateImage)
GO
OpenFileDialog openfileDialog = new OpenFileDialog();
openfileDialog.Filter = "Picture Files(*.jpg)|*.jpg|Bmp(*.bmp)|*.bmp|All Files(*.*)|*.*";
FileStream fileStream;
openfileDialog.ShowDialog();
string filepath = openfileDialog.FileName;
if (filepath.Trim() != "")
{
//获得图象并把图象转换为byte[]
fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
byte[] photoArray = new byte[(int)fileStream.Length];
fileStream.Read(photoArray, 0, photoArray.Length);
fileStream.Close();
try
{
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
pictureBox1.Image = System.Drawing.Image.FromFile(filepath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//连接数据库
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString ="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
SqlCommand mySqlCommand = new SqlCommand("UpdateImage", myConnection);
mySqlCommand.CommandType = CommandType.StoredProcedure;
mySqlCommand.Parameters.Add("@UpdateImage", SqlDbType.Image);
mySqlCommand.Parameters["@UpdateImage"].Value = photoArray;
//如果不使用存储过程,可以把上面四句代码改为:
//string strSql="Insert into test(FImage) values(@FImage)";
//SqlCommand cmd=new SqlCommand(strSql,conn);
//cmd.Parameters.Add("@FImage",SqlDbType.Image);
//cmd.Parameters["@FImage"].Value=photoArray;
myConnection.Open();
mySqlCommand.ExecuteNonQuery();
myConnection.Close();
}
//以下是从数据库读取图片
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=localhost;Database=test;User Id=sa;Pwd=sa";
string strSql = "select FImage from test where id=1";
SqlCommand cmd = new SqlCommand(strSql, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
MemoryStream ms = new MemoryStream((byte[])reader["FImage"]);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
reader.Close();
conn.Close();
pictureBox1.Image = image;