取得文件流及相关信息后存入数据库中HttpPostedFile upFile = up_file.PostedFile;
int iFileLength = upFile.ContentLength;
Byte[] FileByteArray = new Byte[iFileLength];
Stream StreamObject = upFile.InputStream;
StreamObject.Read(FileByteArray, 0, iFileLength);
SqlConnection conn = new SqlConnection("server=yy;uid=sa;pwd=;database=pany");
string sql = "insert into t_imgs (imgData, type, description, imgSize) values " 
+ "(@Image, @ContentType, @ImageDescription, @ImgSize)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Image", SqlDbType.Binary, iFileLength).Value = FileByteArray;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = upFile.ContentType;
cmd.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDesc.Text;
cmd.Parameters.Add("@ImgSize", SqlDbType.BigInt, 8).Value = upFile.ContentLength;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

解决方案 »

  1.   

    OleDbConnection Conn = new OleDbConnection();
    OleDbCommand Cmd = Conn.CreateCommand();
    Cmd.CommandText = "Insert Into t_Goods_Image(Goods_ID, Goods_Image) Values (?, ?)";
    OleDbParameter ParamID = Cmd.Parameters.Add(new OleDbParameter("Goods_ID", System.Data.OleDb.OleDbType.VarWChar, 50, "Goods_ID"));
    OleDbParameter ParamImage = Cmd.Parameters.Add(new OleDbParameter("Goods_Image", System.Data.OleDb.OleDbType.VarBinary, 0, "Goods_Image"));
    ParamID.Value = GoodsID.ToString();

    try
    {
    FileStream ImageStream = File.OpenRead(lblPicPath.Text);
    byte[] bytStream = new byte[ImageStream.Length];
    long lngRead = ImageStream.Length;
    lngRead = ImageStream.Read(bytStream, 0, (int)lngRead);
    ParamImage.Value = bytStream;
    }
    catch(FileNotFoundException Ex)
    {
    MessageBox.Show("未找到指定图像文件!");
    Console.Write(Ex.Message);
    }
    catch(FileLoadException Ex)
    {
    MessageBox.Show("无法读取图像文件!");
    Console.Write(Ex.Message);
    }

    DBConnection.OpenConnection(Conn);
    long lngAffected = Cmd.ExecuteNonQuery();
    Conn.Close();
      

  2.   

    http://expert.csdn.net/Expert/topicview.asp?id=1690497
      

  3.   

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
      

  4.   

    将本地图像文件读成文件流,插入或更新到sqlserver中的image字段中以二进制形式存储。从数据库中读该字段,输出文件流,存成临时文件,有picture控件显示就可以。