C#如何将文件(比如:txt,gif,jpg,doc等格式的文档)以流的方式插入到Image类型的字段中.请附详细代码.谢谢!

解决方案 »

  1.   


    private void button1_Click(object sender, EventArgs e)
            {
                Stream ms;
                byte[] picbyte;
                OpenFileDialog ofdSelectPic = new OpenFileDialog();
                ofdSelectPic.ShowDialog();
                ms = ofdSelectPic.OpenFile();
                picbyte = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));            SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=;database=s");
                conn.Open();
                string sqlstring = "insert into ttt(image) values(@img)";
                SqlCommand cmd = new SqlCommand(sqlstring, conn);
                cmd.Parameters.Add("@img", SqlDbType.Image, picbyte.Length).Value = picbyte;
                
                cmd.ExecuteNonQuery();
                conn.Close();
            }用例中数据库为s, 表名为ttt ,字段名为image, 类型为image的. 
    将选中的图片放入数据库中.
      

  2.   


    string fileAbsolutePath = @"d:\a.gif"; //文件的路径byte[] fileContents = File.ReadAllBytes(fileAbsolutePath);            using (SqlConnection cn = new SqlConnection("Server=.;Uid=sa;Pwd=你的密码;Database=pubs")) //连接数据库
                {
                    using (SqlCommand cmd = new SqlCommand("INSERT INTO test (imageField) VALUES (@img)", cn)) //插入到指定的表的指定的字段
                    {
                        try
                        {
                            cn.Open();
                            cmd.Parameters.Add("@img", SqlDbType.Image).Value = fileContents;
                            cmd.ExecuteNonQuery();
                        }
                        catch (SqlException ex)
                        {
                            throw ex;
                        }
                    }
                }
      

  3.   

    转化为二进制格式再存到Image类型的字段中
      

  4.   

    转化为二进制格式再存到Image类型的字段中
     try
                {
                    FileInfo fi = new FileInfo(filename);
                    if (!fi.Exists)
                    {
                        MessageBox.Show("该文件名不存在");
                    }
                    byte[] contents = new byte[fi.Length];
                    FileStream fs = fi.OpenRead();
                    fs.Read(contents, 0, contents.Length);
                    fs.Close();
                    string str1 = "server=127.0.0.1;database=xu;uid=sa;pwd=''";
                    SqlConnection sqlconn = new SqlConnection(str1);
                    sqlconn.Open();
                    string str3 = "insert into sc(sno,image) values(@sno,@image) ";
                    SqlCommand sqlcomm = new SqlCommand(str3, sqlconn);
                    sqlcomm.Parameters.Add("@sno", SqlDbType.Char, 5);
                    sqlcomm.Parameters.Add("@image", SqlDbType.Image, 16);
                    sqlcomm.Parameters["@sno"].Value = "0521";
                    sqlcomm.Parameters["@image"].Value = contents;
                    sqlcomm.ExecuteNonQuery();
                    sqlconn.Close();
                    MessageBox.Show("图像文件 " + fi.FullName + " 成功上传到数据库!");
                      }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message);
                }