首页 > 开发 > 综合 > 正文

存储图片(C#+Winform+SQL Server)(From xlkun Hacken的专栏)

2024-07-21 02:27:00
字体:
来源:转载
供稿:网友
  • 本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。
  • 把图片读到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();

    -------------------------------------------------------
    从数据库读图片到picturebox

    sqlconnection 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;


    发表评论 共有条评论
    用户名: 密码:
    验证码: 匿名发表