LOGO

LOGO
LOGO
ads header

Breaking News

MS SQL SREAM FILE READ AND SAVA(STREAM DOSYA OKU VE KAYDET)



string cs = @"Data Source=DTBSRV\SQLEXPRESS2014;Initial Catalog=WEBLABFILE;Integrated Security=TRUE";
            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                SqlTransaction txn = con.BeginTransaction();
           

                string sql=@"SELECT file_stream.PathName() as DosyaUrl, GET_FILESTREAM_TRANSACTION_CONTEXT() as DosyaStream, Name,(CAST([stream_id] AS VARCHAR(36))+'.' +[file_type]) AS STR_stream_id FROM[WEBLABFILE].[dbo].[MyFileTables]
              WHERE
              [WEBLABFILE].[dbo].[MyFileTables].[stream_id] IN(SELECT[stream_id]   FROM[WEBLABFILE].[dbo].[UPLOADFILES]   WHERE[ID_ANALIZ] = 1)";

                //(CAST([stream_id] AS VARCHAR(36))+'.' +[file_type]) AS STR_stream_id

                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();
                    string NewfName = rdr[3].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 = Server.MapPath(@"~/Temp/") + NewfName;
                    Response.Write(Server.MapPath(@"~/Temp/") + NewfName + "<br>");


                   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();


Hiç yorum yok