M
M
Maria Popova2015-12-03 12:31:16
ASP.NET
Maria Popova, 2015-12-03 12:31:16

How to access a file in Visual Studio 2015?

Essence of the question: there is an img folder, a picture is loaded into it using FileUpload (the picture appears in the folder, but not in the browser), then in the "Add" button handler, the picture should be converted to binary type and placed in the database (field type varbinary (max) ). BUT an error occurs that there is no access to the file, how to get this access?
A project that is created in VS 2008 works fine, but in VS 2015 it throws an error.

// Создать подключение к базе
string connectionString =
WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);

// Загрузить данные таблицы в DataReader
SqlCommand command = new SqlCommand(
"INSERT INTO Photo_DJ ( Photo, Id_photo_dj_dj, Type_photo) VALUES (@Photo, @Type_Ph, @DJ)",
connection);
Stream fs = null;
try
{
connection.Open();

// Указать параметр
command.Parameters.AddWithValue("@DJ", GVDJ.SelectedValue);


//добавляем рисунок
Byte[] bytes = null;
string contenttype = String.Empty;

fs = (Stream)Session["InputStream_FileUpload"];


BinaryReader br = new BinaryReader(fs);

bytes = br.ReadBytes((Int32)fs.Length);
contenttype = (string)Session["contenttype"];
command.Parameters.Add("@photo", SqlDbType.VarBinary).Value = bytes;
command.Parameters.Add("@typephoto", SqlDbType.NVarChar).Value = contenttype;

// cmd = new SqlCommand("insert into empimage(Name) values('" + "~/Image/" + FileUpload1.FileName + "')", con); con.Open(); cmd.ExecuteNonQuery(); con.Close();

int res = command.ExecuteNonQuery();

fs.Close();
lbl_answerquery.Text = "INSERT ok, count rows affected = " + res;
}
catch (Exception ex)
{
lbl_answerquery.Text = "INSERT error = " + ex.ToString();
}
finally
{
if (fs != null)
fs.Close();
connection.Close();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Ishchenko, 2015-12-03
@sepo

Storing pictures in a database is a bad approach.
For data, use the database, and for files, use the file system. Try storing the file name in the database and when you need it, take it from disk. This will give significant advantages: reducing the query time to the database and the amount of information transferred from there, reading from the file system can be more optimized in terms of speed.
Also, do not store large data in the session - they will take up space in the RAM on the server for the lifetime of the session.
Try the Entity Framework - you won't need to manually write sql queries, just work with classes - it takes care of all the work with the database
If the file is not visible in the browser in Visual Studio, select Add->Existing item (or Alt+Shift+A) from the context menu of the folder in the browser.
look at metanit.com and professorweb.ru - there are a lot of materials on which you can deal with modern .net technologies.
also there are many resources on asp.net

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question