Hi there,

I want to upload files with the extension pdf, doc excel and images to the MS SQL database from a C# desktop application. I am using C#.net and not asp. I want to upload a file from a interface to the ms sql database,
Hey can someone guide me with this , because I have no idea about this I searched in the internat but there are application on web based application but not for desktop application can some one fine me a tutorial or a project to do this
Thanxx
Appreciate a lot

What are you uploading to?

There are different methods for transferring data to a website or to a custom built server application.

One of them will use a web request style system, the other will use TcpClient/TcpServer.

What are you uploading to?

There are different methods for transferring data to a website or to a custom built server application.

One of them will use a web request style system, the other will use TcpClient/TcpServer.

what is the difference in between those,

w@t i wasnt to do is out a copy of the file in the database in any way and get back the file opening it with the relevant software(eg: if it is a doc file to open it with a word document).

and also i am trying to upload the file to a table in the MS SQL database.
thankxxxx

Well ok, you need to decide a couple of things. The first being "What is the server?"

Are you trying to pull data from an existing website or is it just some remote machine?

try this link
click here

Hey I want to upload and download file from a windows from interface not from a web site do u have relating tutorials for this,

Thanxxx
Appreciate a lot

Well, you're going to need to write two programs.

One will be a server. It will run on the machine that is hosting the database.

One will be a client. It will run on your local machine.

You should Google for "Client Server Tutorial C#" for a tutorial of how to communicate over a network.

In your database you will also need a table containing a list of FileTypes. In the table that holds your files, you should have a "FileTypeId" field which holds the ID of which file type your data is.

So lets say your Database definition is as such: CREATE TABLE DataFiles(Id int(3) primarykey notnull, FileData binary notnull, FileTypeId int notnull); CREATE TABLE FileTypes(Id int(3) primarykey notnull, FileType char(255)); (note: My SQL from memory isn't perfect but it should give you a good basis)

You would populate the FileTypes table with things like "DOC", "PDF", "XLS" etc. So when you insert your file data into the database you would set the FileTypeId to the relevant row in your FileTypes table.

Ok that's the easy part. The hard part is transferring this data around. Look up the tutorials for a Client/Server program. There's a lot around.

Once you've created the client/server you need to decide on a protocol. This is basically a standard way of knowing how your programs should talk to each other.

So for example one might be:
Int64 MessageLength
Int32 FileType
Byte[MessageLength] FileData

So every time you send or receive, it must be in that format. That way, when it gets to the other end, you know what to expect.

Once that's done, it's simply a matter of opening the file (in binary mode) reading the file in byte by byte and then sending it across the connection.

From the other way, you get the binary data from the database and send it to the client.

Projects like these are quite fun. =)

Well ok, you need to decide a couple of things. The first being "What is the server?"

Are you trying to pull data from an existing website or is it just some remote machine?

hey currently the server is MS SQL 2005 and it is in the local machine,probably it will go be installed in a remote computer

thanx

Well, you're going to need to write two programs.

One will be a server. It will run on the machine that is hosting the database.

One will be a client. It will run on your local machine.

You should Google for "Client Server Tutorial C#" for a tutorial of how to communicate over a network.

In your database you will also need a table containing a list of FileTypes. In the table that holds your files, you should have a "FileTypeId" field which holds the ID of which file type your data is.

So lets say your Database definition is as such: CREATE TABLE DataFiles(Id int(3) primarykey notnull, FileData binary notnull, FileTypeId int notnull); CREATE TABLE FileTypes(Id int(3) primarykey notnull, FileType char(255)); (note: My SQL from memory isn't perfect but it should give you a good basis)

You would populate the FileTypes table with things like "DOC", "PDF", "XLS" etc. So when you insert your file data into the database you would set the FileTypeId to the relevant row in your FileTypes table.

Ok that's the easy part. The hard part is transferring this data around. Look up the tutorials for a Client/Server program. There's a lot around.

Once you've created the client/server you need to decide on a protocol. This is basically a standard way of knowing how your programs should talk to each other.

So for example one might be:
Int64 MessageLength
Int32 FileType
Byte[MessageLength] FileData

So every time you send or receive, it must be in that format. That way, when it gets to the other end, you know what to expect.

Once that's done, it's simply a matter of opening the file (in binary mode) reading the file in byte by byte and then sending it across the connection.

From the other way, you get the binary data from the database and send it to the client.

Projects like these are quite fun. =)

hey thank you for the information,
by the way i am using a desktop application to upload and download files(i am trying to use) do i have to use ASP.NET ???
i am using windows form application at the moment,
is the client server thing supporting desktop application???


thanx

Yes there's no reason to use ASP.NET. Look on Google for the things I told you and it will tell you how to do it.

Specifically TcpClient and TcpServer. (At the very bottom of that is the Socket class)

Yes there's no reason to use ASP.NET. Look on Google for the things I told you and it will tell you how to do it.

Specifically TcpClient and TcpServer. (At the very bottom of that is the Socket class)

hey when i search client server tutorial in C#, all the links are relating to ASP.NET, (which is web application right)

how should i look up in google
thanxxxx

Google

None of the first page links refer to ASP...

Google

None of the first page links refer to ASP...

hey thanxxxxxxxxxx

I am guessing that you want to read a file from the computer where your app is running and store in the SQL DB.
You will need to read the file in to a byte array using System.IO.File.ReadAllBytes("filename"); Then pass this as a parameter to a varbinary(MAX) column in you DB.
Don't forget to store the filename and extension (or type code as Ketsuekiame recommended).
If you store them seperately you will later be able to sort/filter on file type much easier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.