But how could you live and have no story to tell!
访问统计 original graphics

有一个Temp1表,其中一列Pic为Image类型,想把这个列中的数据存入另一个表中Temp2中的Pic类型。该怎么做呢?代码如下:

SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
        myconn.Open();
        SqlCommand mycmd= new SqlCommand();
        mycmd.Connection = myconn;
        mycmd.CommandText = "select Pic from Temp1 where ID=1" ;
        SqlDataReader mydr= mycmd.ExecuteReader();
        if(mydr.Read())
        {
            byte[] buffer = new Byte[10240];
            long datalen = mydr.GetBytes(0, 0, null, 0, 0);//0相当于mydr["Pic"]
            long curPos = 0;
            long readsize = 0;
            readsize = mydr.GetBytes(0, curPos, buffer, 0, 10240);
            while (readsize == 10240)
            {
                curPos += readsize;
                readsize = mydr.GetBytes(0, curPos, buffer, 0, 10240);
            }
            byte[] rBuf = new Byte[curPos + readsize];
            mydr.GetBytes(0, 0, rBuf, 0, (int)(curPos + readsize));

         }

         mydr.Close();

         mycmd.CommandText = "INSERT INTO Temp2 (Pic)  VALUES(@FileContent)";
         mycmd.Parameters.Add("@FileContent", System.Data.SqlDbType.Image).Value = rBuf;
         myconn.Close();

posted on 2008-08-18 16:32  nextsoft  阅读(478)  评论(0编辑  收藏  举报