如何把图片存到SQLSERVER中?能提供代码么,多谢!

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/4154/4154802.xml?temp=.2382776
      

  2.   

    这样的东西太多了,楼主在网上随便搜索,就是在CSDN里面搜索,都有很多你需要的东西的。
    建议先自己找找。
      

  3.   

    /// <summary>
    /// Creat New File in DB
    /// </summary>
    /// <param name="strNodeId"></param>
    /// <param name="file"></param>
    /// <param name="description"></param>
    /// <param name="userId"></param>
    public void creatNewFile(string strNodeId,HtmlInputFile file,string description,string userId)
    {
    Stream imgdatastream = file.PostedFile.InputStream;
    string fileName = System.IO.Path.GetFileName(file.PostedFile.FileName).Trim();
    int imgdatalen = file.PostedFile.ContentLength;
    string imgtype = file.PostedFile.ContentType;
    byte[] imgdata = new byte[imgdatalen];
    int n = imgdatastream.Read(imgdata,0,imgdatalen);
    DataBase db = new  DataBase();
    DataSet ds ;
    db.Open();

    // check DB
    string  sqlcheck  = " SELECT * FROM T_DMS_DOC ";
    sqlcheck += " WHERE NodeId  = '"+ DataBase.SqlItemTextChange(strNodeId) +"'";
    sqlcheck += " AND   DocName = '"+ DataBase.SqlItemTextChange(fileName) +"'";
    sqlcheck += " AND   DelStatus = 0"; // not deleted

    db.ExecSQL(sqlcheck,"T_DMS_DOC",out ds); if(ds.Tables.Count!= 0 && ds.Tables[0].Rows.Count !=0)
    {
    throw new Exception("1011");
    }

    // Get Document ID
    double randomStr = new System.Random().NextDouble()* 1000000000;
    string docID = DateTime.Now.ToString("yyyyMMdd") + (Math.Floor(randomStr)).ToString(); // Insert File to DB
    string sql  = " INSERT INTO T_DMS_DOC (NodeId,DocId,Version,DocName,Content,Description,UpdDate,UpdUid,CheckOut,DelStatus) ";
    sql += " VALUES( '"+ DataBase.SqlItemTextChange(strNodeId) +"','"+docID+"',1,'"+DataBase.SqlItemTextChange(fileName) +"',@imgdata,'"+DataBase.SqlItemTextChange(description)+"',GETDATE(),'"+DataBase.SqlItemTextChange(userId)+"',"+"'0','0')" ; SqlCommand command = new SqlCommand(sql,db.Connection ); SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
    paramData.Value = imgdata;
    command.Parameters.Add( paramData ); try
    {
    // db.BeginTrans();
    // Write Log
    LogManager.WriteLog("Execute  "+command.CommandText); int numRowsAffected = command.ExecuteNonQuery(); if(numRowsAffected !=1)
    {
    throw new Exception("1012");
    } LogManager.WriteLog("Success"); //db.CommitTrans();
    }
    catch(Exception ex)
    {
    LogManager.WriteLog("Failure  "+ ex.Message);
    db.RollBack();
    throw ex;
    }
    finally
    {
    db.Close();
    }
    }