Skip to content

How to save RTF file in binary or image field in SQL Server?

Ersin Miman

Asked Jun 16, 2007, 1:37 AM EDT5 answers
Share
LinkedInX
hi all,

I want to save content of RichTextBox (image, text etc.) to in binary or image field in SQL Server, but I couldn't...

Please help me, thank you

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Java
    FileStream inFile;
    Byte[] binaryData;
    try
    {
    inFile = new FileStream("ersin miman", FileMode.Open, FileAccess.Read);
    binaryData = new Byte[inFile.Length];
    }
    catch (Exception exp)
    {
    // Error creating stream or reading from it.
    System.Console.WriteLine("{0}", exp.Message);
    return;
    }
    
    sqlConnection1.ConnectionString = "Data Source=localhost;Initial Catalog=dbSoru;User ID=sa;Password=xxxxxx";
    sqlConnection1.Open();
    SqlTransaction trs = null;
    try
    {
    trs = sqlConnection1.BeginTransaction();
    sqlCommand1.Connection = sqlConnection1;
    sqlCommand1.CommandText = "INSERT INTO tbT_Soru(SoruBinary) VALUES('" + binaryData + "')";
    sqlCommand1.Transaction = trs;
    sqlCommand1.ExecuteNonQuery();
    trs.Commit();
    }
    catch (Exception e1)
    {
    if (trs != null)
    {
    trs.Rollback();
    }
    }
    sqlConnection1.Close();
    0
  • 0

    Can I use these method to save and read to/from binary field to SQL Server?


    MemoryStream ms_memStream = new MemoryStream();

    BinaryWriter br_binaryWriter = new BinaryWriter(ms_memStream);

    br_binaryWriter.Write("sample text");



    But, I don't know how will to use it?

    sqlCommand1.CommandText = "INSERT INTO tbT_Soru(SoruBinary) VALUES(" + ms_memStream.ToString() + ")";   ????????

    0
  • 0
    the data from your file is never stored in de byte array, use
    Code
    
    inFile.Read(binaryData, 0, length);
    0
  • 2 short replies
    • 0
      how did you tried to do that?
      0