2

I am using Sql-Server 2012. In it i made a column named Image with datatype image in Person table. Please explain that how can i add value in it? I mean should i have to define a path of any photo? or is there any other way to insert images in this column? Looking forward to your guidance

Person --Table name person_name -- Column name with datatype nvarchar(50) Image -- Column name with datatype image Date -- Column name with datatype date

Or insertion is like: `

INSERT INTO Person  Values
( 'Person34', '  
SELECT  *  
       FROM OPENROWSET  
      ( BULK 'C:\Users\Public\Pictures\Sample Pictures\Lighthouse.jpg',SINGLE_CLOB)' , '2013-09-08');

` Thank You.

1 Answer 1

1

this is how I do it to save the image inside the BLOB/Image DB field.

/// <summary>
/// Saves the file into a BLOB/Image field in the DB
/// This uses an UPDATE command, therefore the record must alreay exist in the DB
/// </summary>
/// <param name="aTableName"></param>
/// <param name="aFieldName"></param>
/// <param name="aWhereClause"></param>
/// <param name="aFileName"></param>
/// <returns></returns>
public bool SaveToBLOB(string aTableName, string aFieldName, string aWhereClause, string aFileName)
{
  string sSQL = string.Format("UPDATE {0} SET {1}=@{1} WHERE {2}", aTableName, aFieldName, aWhereClause);
  using (SqlCommand oComm = new SqlCommand(sSQL, m_Conn))
  {
    byte[] wFileAsByteArr = CDBConn.GetFileAsByteArray(aFileName);

    oComm.Parameters.Add("@" + aFieldName, SqlDbType.Image, wFileAsByteArr.Length).Value = wFileAsByteArr;
    return oComm.ExecuteNonQuery() > 0;
  }
}

And this is how I retrieve it: /// /// extract the first field (BLOB) of the specified SQL Command and save it as a file /// /// /// /// true if the file was created and false otherwise public bool ExtractBLOB(string aSQLCommand, string aFileName) { using (SqlCommand oComm = CreateCommand()) { oComm.CommandText = aSQLCommand; oComm.CommandType = CommandType.Text;

        // Create a file to hold the output.
        using (System.IO.FileStream fs = new System.IO.FileStream(aFileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
        {
            using (System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs))
            {
                byte[] b = (byte[])oComm.ExecuteScalar();
                if (b != null)
                {
                    bw.Write(b);
                    bw.Flush();
                    return true;
                }
                else
                    return false;
            }
        }
    }
}

Where the aSQLCommand will be: SELECT MyImageField FROM TableABC WHERE ...

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for sharing but i want to execute query in sql-server 20102.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.