Posta la tua soluzione, spiegandola, in modo che altri che avranno il tuo stesso problema riescano a risolverlo.
Posta la tua soluzione, spiegandola, in modo che altri che avranno il tuo stesso problema riescano a risolverlo.
int rowsAffected;
bool controllo = false;
string queryContr = ("SELECT Id FROM tabella WHERE id = " +id); // hasRows..
string filepath = (".pdf");
SqlConnection con = new SqlConnection(Properties.Settings.Default.Connecti onStr);
SqlCommand cmd = new SqlCommand("UPDATE tabella SET campo=@campo WHERE id= " + id, con);
FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
Byte[] b = new Byte[fs.Length];
fs.Read(b, 0, b.Length); // legge da filestream e riempie un buffer
fs.Close();
SqlParameter p = new SqlParameter("@campo", SqlDbType.VarBinary, b.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, b);
cmd.Parameters.Add(p);
// Query di controllo prima della transazione.
con.Open();
SqlDataReader reader = new SqlCommand(queryContr, con).ExecuteReader();
if (reader.HasRows)
{
controllo = true;
con.Close();
}
else
{
Console.WriteLine("Per id= " + id + " record non presente ");
Console.ReadKey(true);
}
if (controllo) // esegue transazione
{
con.Open();
rowsAffected = cmd.ExecuteNonQuery();
Console.WriteLine("Pdf inserito RowsAffected:" + rowsAffected);
con.Close();
Console.ReadKey(true);
}
}