Datoteke držim u varbinary(max) polju u tabeli.
code za spremanje u bazu
Code:
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
Byte[] fileAsByte = new Byte[fs.Length];
fs.Read(fileAsByte, 0, Convert.ToInt32(fs.Length));
SqlConnection konekcija = new SqlConnection(RazneDatotekeBaza.Properties.Settings.Default.testnaCS);
SqlCommand dodajFile = new SqlCommand("update unosDatoteka set datoteka = @FileStream where sifra = '" +
unosDatotekaDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString().Trim() + "'", konekcija);
SqlParameter imageParameter = dodajFile.Parameters.Add("@FileStream",SqlDbType.VarBinary);
imageParameter.Value = fileAsByte;
imageParameter.Size = fileAsByte.Length;
konekcija.Open();
dodajFile.ExecuteNonQuery();
konekcija.Close();
fs.Close();
FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
Byte[] fileAsByte = new Byte[fs.Length];
fs.Read(fileAsByte, 0, Convert.ToInt32(fs.Length));
SqlConnection konekcija = new SqlConnection(RazneDatotekeBaza.Properties.Settings.Default.testnaCS);
SqlCommand dodajFile = new SqlCommand("update unosDatoteka set datoteka = @FileStream where sifra = '" +
unosDatotekaDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString().Trim() + "'", konekcija);
SqlParameter imageParameter = dodajFile.Parameters.Add("@FileStream",SqlDbType.VarBinary);
imageParameter.Value = fileAsByte;
imageParameter.Size = fileAsByte.Length;
konekcija.Open();
dodajFile.ExecuteNonQuery();
konekcija.Close();
fs.Close();
code za spremanje fajla na disk i njegovo otvaranje:
Code:
SqlConnection konekcija2 = new SqlConnection(RazneDatotekeBaza.Properties.Settings.Default.testnaCS);
SqlCommand cmdSelect = new SqlCommand("select datoteka from unosDatoteka where sifra = '" +
unosDatotekaDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString().Trim()+ "'", konekcija2);
konekcija2.Open();
byte[] barrImg = (byte[])cmdSelect.ExecuteScalar();
konekcija2.Close();
string strfn = String.Format("{0}{1}{2}", System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetModule("RazneDatotekeBaza.exe").FullyQualifiedName).ToString(),
"\\Temp\\", unosDatotekaDataGridView.Rows[e.RowIndex].Cells[2].Value.ToString().Trim());
FileStream fsx = new FileStream(strfn, FileMode.Create,FileAccess.ReadWrite);
fsx.Write(barrImg, 0, barrImg.Length);
fsx.Flush();
fsx.Close();
Process.Start(strfn);
SqlConnection konekcija2 = new SqlConnection(RazneDatotekeBaza.Properties.Settings.Default.testnaCS);
SqlCommand cmdSelect = new SqlCommand("select datoteka from unosDatoteka where sifra = '" +
unosDatotekaDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString().Trim()+ "'", konekcija2);
konekcija2.Open();
byte[] barrImg = (byte[])cmdSelect.ExecuteScalar();
konekcija2.Close();
string strfn = String.Format("{0}{1}{2}", System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetModule("RazneDatotekeBaza.exe").FullyQualifiedName).ToString(),
"\\Temp\\", unosDatotekaDataGridView.Rows[e.RowIndex].Cells[2].Value.ToString().Trim());
FileStream fsx = new FileStream(strfn, FileMode.Create,FileAccess.ReadWrite);
fsx.Write(barrImg, 0, barrImg.Length);
fsx.Flush();
fsx.Close();
Process.Start(strfn);
unosDatotekaDataGridView među ostalima ima kolone sifra (primarni kljuc,index kolone 0 ) i dokument ( originalni naziv dokumenta , npr. moj.pdf , sa indexom kolone 2).
Nigdje se ne pojavljuje nikakva greska, a recimo ako otvaram pdf foxit readerom foxit javlja: not a pdf or corrupted.
Gdje grijesim?