Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
i wants to store and retrieve file in sql server db
the code of store work probably. but code of retrieve would be faced to this error:

'GET_FILESTREAM_TRANSACTION_CONTEXT' is not a recognized built-in function name"

code of retrieve :
C#
private void btnOpen_Click(object sender, EventArgs e)
{
  string cs = @"Data Source=<your server="">;Initial Catalog=MyFsDb;Integrated Security=TRUE";

  using (SqlConnection con = new SqlConnection(cs))
  {
    con.Open();
    SqlTransaction txn = con.BeginTransaction();
    string sql = "SELECT fData.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT(), fName FROM MyFsTable";
    SqlCommand cmd = new SqlCommand(sql, con, txn);
    SqlDataReader rdr = cmd.ExecuteReader();
    
    while (rdr.Read())
    {
      string filePath = rdr[0].ToString();
      byte[] objContext = (byte[])rdr[1];
      string fName = rdr[2].ToString();
 
      SqlFileStream sfs = new SqlFileStream(filePath, objContext, System.IO.FileAccess.Read);
 
      byte[] buffer = new byte[(int)sfs.Length];
      sfs.Read(buffer, 0, buffer.Length);
      sfs.Close();
 
      // Just write all files in the table to the temp direcotory.
      // This is probably not how you would do it in the real world. But this is just an example.
      string filename = @"C:\Temp\" + fName;
 
      System.IO.FileStream fs = new System.IO.FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write);
      fs.Write(buffer, 0, buffer.Length);
      fs.Flush();
      fs.Close();
    }
 
    rdr.Close();
    txn.Commit();
    con.Close();
  }
}
Posted
Updated 4-Dec-14 5:03am
v2
Comments
Kornfeld Eliyahu Peter 4-Dec-14 11:01am    
What version of SQL you have?

1 solution

Try:
string sql =
"SELECT fData.PathName(), dbo.GET_FILESTREAM_TRANSACTION_CONTEXT(), fName FROM MyFsTable";
/ravi
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900