Save file and path? [closed]

0

I have my following code:

public   class UsrConexion
{
public string cadenaconexion;
protected string sql; 
protected int resultado;
protected SqlConnection cnn;
protected SqlCommand comandosql;
protected string mensaje;
protected string sql1;

public UsrConexion()
    {
    this.cadenaconexion = ("data source=ECRIBOL\Juan360;initial catalog=Degi;user id=sa;password=12stones.4;MultipleActiveResultSets=True");
    this.cnn = new SqlConnection(this.cadenaconexion);

}

        }
}'

I have my 2 tables Invoices and Buyer

What I want is to save the data, including the pdf file and the id of the Buyer table on a server and in the sql save your route. I hope you can help me. (I just want to do it through a consultation or a small example, I'm a novice and I've never worked with files, much less with routes)

    
asked by SoyKrut 23.11.2017 в 23:07
source

1 answer

1

Well look, I did not understand well. If what you want is to save the file, what you can do is:

use the OpenFileDialog function, to find the document you want to save. For example:

OpenFileDialog open = new OpenFileDialog();
        open.Filter = "Archivos PDF|*.pdf";
        open.Title = "Abrir";
        if (open.ShowDialog() == DialogResult.OK)
        {

            txtFileName.Text = open.FileName;
        }
        open.Dispose();

That way, you save the route in a textbox to be able to see the route and then use it. Then to prepare it to be saved in the bd, you use the System.IO library. It is best to save the files in varbinary (max) format. Continuing, you declare a byte type arrangement that is what resembles a varbinary, then matches it to a method of the System.IO library called File, when writing a File. You will see a series of methods that you have. what you need is to pass it to an array of bytes then you use, File.ReadAllBytes (txtFileName.Text) where txtFileName.Text is already going to have the path that you picked up from the filedialog.

In the end you got something like this:

byte[] pdf = File.ReadAllBytes(txtFileName.Text);

then in a sql query you pass the array as a parameter for the "Document" field in your database. (I did not remember the name of the field so I named it Document).

If you only want to save the route, you only pass the parameter that has txtFileName.Text

I hope it serves you.

    
answered by 24.11.2017 в 20:07