C#+SQL数据库 保存和显示图片

作者在 2008-07-22 14:37:10 发布以下内容

显示图片:

                        string id = textBox1.Text;             

                       ERMSData str = new ERMSData("server=Sunny; database=people; trusted_connection=true");             //ERMSData 是建立的连接数据库的一个自定义类.

                      SqlConnection connection = new SqlConnection(str.connectionString);
                      SqlCommand cmd = new SqlCommand("select PHOTO from t_xinxi WHERE ID = '" + i + "'");
                      cmd.Connection = connection;
                      connection.Open();
                      byte[] b = (byte[])cmd.ExecuteScalar();                //强制转换为byte[] 类
                       if (b.Length > 0)
                          {
                              MemoryStream stream = new MemoryStream(b, true);
                              stream.Write(b, 0, b.Length);
                              pictureBox1.Image = new Bitmap(stream);
                              stream.Close();
                          }

                        connection.Close();

保存(不能保存GIF格式的):
              string id = textBox1.Text;   
              openFileDialog1.ShowDialog();
              string mp =openFileDialog1.FileName;
              string sql = "update t_xinxi set PHOTO=@mp where ID='" + id + "'";

              pictureBox1.Image = new Bitmap(@mp);                        //显示选择的图片
              FileStream fs = File.OpenRead(mp);                            //读图象文件到流文件
              byte[] b = new byte[fs.Length];                                    //根据该流文件的大小生成byte[] 类
              fs.Read(b, 0, b.Length);                                             //流文件类对象将字节块写到缓冲区
              fs.Close();                                                                 //关闭该流对象

              ERMSData nn = new ERMSData("server=Sunny; database=people; trusted_connection=true");
              SqlConnection connect = new SqlConnection(nn.connectionString);
              SqlCommand     cmd1=new     SqlCommand(sql);
              cmd1.Connection = connect;                                            //数据库连接

              cmd1.Parameters.Add("@mp", SqlDbType.Image).Value = b;                 
              cmd1.Connection.Open();
              cmd1.ExecuteNonQuery();                                                //入库

              cmd1.Connection.Close();

除了可以在数据库存图象文件外,这种方法还可以存储其他类型,如下是保存文本文件的:

              string id = textBox1.Text;          
              openFileDialog1.ShowDialog();
              string fp = openFileDialog1.FileName;
              string str = "update t_xinxi set GWEI=@fp where ID ='" + id + "'";
             StreamReader S=new StreamReader(fp);                       //读指定文件的StreamReader 对象
             string LL = S.ReadLine();

             ERMSData dd = new ERMSData("server=Sunny; database=people; trusted_connection=true");
              SqlConnection connet = new SqlConnection(dd.connectionString);
              SqlCommand cmd1 = new SqlCommand(str);
              cmd1.Connection = connet;
              cmd1.Parameters.Add("@fp", SqlDbType.NVarChar).Value = LL ;
              cmd1.Connection.Open();
              cmd1.ExecuteNonQuery();  
              cmd1.Connection.Close();

C# | 阅读 4962 次
文章评论,共0条
游客请输入验证码
浏览33941次
最新评论