首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

急如何把数据库的img提出为string或者txt

2013-06-19 
急急急!怎么把数据库的img提出为string或者txtusing Systemusing System.Collections.Genericusing Syst

急急急!怎么把数据库的img提出为string或者txt
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GetImage
{
    class Program
    {
        static void Main(string[] args)
        {
            private void GetImage()
            {
                string conn ="Server=(local);User id =sa;Pwd =huiheng518;dataset =test";
                SqlConnection sqlCon = new Sqlconnection(conn);
                string sql = "SELECT id,pic From Piclist where id=1";
                SqlCommand myCommand = new SqlCommand();
                myCommand.Connection = sqlCon;
                myCommand.CommandType = CommandType.Text;
                myCommand.CommandText = sql;
                DataTable myTable = new DataTable();
                SqlDataAdapter myDataAda = new SqlDataAdapter();
                myDataAda.SelectCommand = myCommand;
                try
                {
                sqlCon.Open();
                myDataAda.Fill(myTable);
                sqlCon.Close();
                    if(myTable.Rows.Count>0)
                    {
                    byte[] image = (byte[])myTable.Row[0]["ImageFile"];
                    string ll = Encoding.Default.GetString(image);
                        WriteStr(@"F:\test.txt",ll);
                    }
                }
                catch(Exception ex)
                {


                string err =ex.Message;
                }
                finally
                {
                sqlCon.Close();
                    myCommand.Dispose();
                    myDataAda.Dispose();
                }
            }
        private void WriteStr(string strLogFileName,string strLogContent)
        {
        try
        {
        FileInfo objFileInfo = new FileInfo(strLogFileName);
            using(FileStream objFileStream =objFileInfo.OpenWrite())
            {
            using (StreamWriter objStreamWriter = new StreamWrite())
            {
            objStreamWriter.BaseStream.Seek(0,SeekOrigin.End);
                objStreamWriter.Writer("{0}",strLogContent);
            }
            }
        }
            catch
        {
            }
        }

        string ss = ReadStr(@"F:\test.txt");
        byte[] b = Encoding.Default.GetBytes(ss);
        string conn ="Server=(local);User id =sa;Pwd =huiheng518;dataset =test";
        SqlConnection sqlCon = new SqlConnection(conn);
        string sql = "update Piclist set ImageFile=@img where id = 1";
        SqlCommand myCommand = new SqlCommand();
        SqlParameter sp = new SqlParameter("@img",SqlDbType.Image);
        myCommand.Connection = sqlCon;
        myCommand.CommandType = CommandType.Text;
        myCommand.CommandText = sql;
        sp.Value = b;
        myCommand.Parameters.Add(sp);
        try


        {
         sqlCon.Open();
        myCommand.ExecuteNonQuery();
        sqlCon.Close();
        }
        catch(Exception eS)
        {
        string ee = eS.Message;
        }
         finally
        {
      sqlCon.Close();
      myCommand.Dispose();

    }
    }
}
[解决办法]
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组 
Stream StreamObject = UpFile.InputStream; //建立数据流对像 
 //读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度 
StreamObject.Read(FileByteArray,0,FileLength); 
 //建立SQL Server链接 
SqlConnection Con = new SqlConnection("Persist Security Info=False;User ID=sa;Initial Catalog=StudyTestData;Data Source=."); 
 String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)"; 
 SqlCommand CmdObj = new SqlCommand(SqlCmd, Con); 
 CmdObj.Parameters.Add("@Image",SqlDbType.Binary, FileLength).Value = FileByteArray; 
 CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar,50).Value = UpFile.ContentType; //记录文件类型 
 //把其它单表数据记录上传 
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar,200).Value = txtDescription.Text; 
 //记录文件长度,读取时使用 
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt,8).Value = UpFile.ContentLength; 
 Con.Open(); 
 CmdObj.ExecuteNonQuery(); 
 Con.Close(); 
 txtMessage.Text = "<p><b>OK!你已经成功上传你的图片</b>"; 
 //int ImgID = Convert.ToInt32(Request.QueryString["ImgID"]); //ImgID为图片ID 
 int ImgID=2;
 //建立数据库链接 
SqlConnection Con = new SqlConnection("Persist Security Info=False;User ID=sa;Initial Catalog=StudyTestData;Data Source=."); 
 String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = @ImageID"; 
 SqlCommand CmdObj = new SqlCommand(SqlCmd, Con); 
 CmdObj.Parameters.Add("@ImageID", SqlDbType.Int).Value = ImgID; 
 Con.Open(); 
 SqlDataReader SqlReader = CmdObj.ExecuteReader(); 
 SqlReader.Read(); 
 //Response.ContentType = (string)SqlReader["ImageContentType"];//设定输出文件类型 
 //输出图象文件二进制数制 
Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]); 


 Response.End(); 
 Con.Close(); 

热点排行