Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

In my application i stored list of files from a directory in an array. I want to store that files with seperate id, name and path in mysql database. How to store that files individually in mysql?

My code is:

string [] files = Directory.GetFiles(@"C:\Temp\","*.wav");

Please help me.

Thanks in advance.
Posted
Comments
CRDave1988 20-Feb-12 3:15am    
what is ur main problem u dont know how to save file in mysql database or unique ID or something else.

The INSERT statement can provide multiple rows at a time, in both MsSQl and MySql:
SQL
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...) (nextValue1, nextValue2, ...)
So in this case you can assemble a command string which will insert many rows - normally I would not recommend this, as you should use parametrised queries to avoid SQL Injection attacks. In this case, you should probably check names and paths to make sure they do not contain SQL!

Use a StringBuilder, and assemble SQL in a loop:
C#
StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO myTable (filename, path) VALUES ");
foreach (string file in files)
    {
    string path = ...;
    string name = ...;
    sb.AppendFormat("('{0}', '{1}') ", name, path);
    }
string sqlCommand = sb.ToString();
Then all you have to do is break the file into the name and path. If you have set your DB to use an Identity field for the Index, it will handle that for you.
 
Share this answer
 
Comments
Varun Sareen 20-Feb-12 3:22am    
very good
As Solution 1 outline the solution, in addition to that you might consider to use SqlParameter instead of passing hard coded Sql string. You might see this link
Configuring Parameters and Parameter Data Types[^] to see usage of the SqlParameter.

Hope it helps :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900