1. How to retrieve stored file our of the database?
have stored (for example) a pdf file into database, as a data type of varbinary(MAX) - binary data. How can I get this file back, like it was before?
Important things I have stored for the particular file are:
- ID
- File name
- File type (pdf, word, ... document)
- and of course the content (which is now in binary data)
2. One more small thing:
I would like to get the ID from a Method1 which retrieves the ID, into another Method2 (in the same class), where I will use this ID as a variable.
I have this code for examle:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int IDDocument; //How to get into IDDocument an ID value from the GetID method (from int Doc_ID)
GetID(IDDocument); //I got an error here: Use of unassigned local variable 'IDDocument
string SQL = "SELECT Content FROM Documents WHERE DocumentID = '" + IDDocoment + "'";
//...
}
private void GetID(int Doc_ID)
{
string DocID = "SELECT DocumentID FROM Documents WHERE Name = '" + listBox1.SelectedItem + "'";
SqlCommand cmd = new SqlCommand(DocID, sqlConn);
cmd.CommandType = CommandType.Text;
sqlConn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
Doc_ID = reader.GetInt32(0);
}
reader.Close();
sqlConn.Close();
}