以下是我写的遍历目录写数据库的例子
你需要改动的就是foreach的具体操作
改称node的添加语句就可以了
关键的思路就是方法的反复调用
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;namespace pickUp
{
/// <summary>
/// pickUp 的摘要说明。
/// </summary>
class pickUp
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
System.Console.WriteLine("Processing...");
System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection("server=(local)\\NetSDK;database=Veimax;Trusted_Connection=yes");
string myString = "DROP TABLE audio; CREATE TABLE audio (id INT IDENTITY(1,1), path VARCHAR(200))";
SqlCommand myCommand = new SqlCommand(myString, myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
getFiles("e:\\共享");
System.Console.WriteLine("...Compeleted");
} static void getFiles(string Path)
{
string[] Directories = System.IO.Directory.GetDirectories(Path);
foreach (object Directory in Directories)
{
Path = (string) Directory;
string[] Files = System.IO.Directory.GetFiles(Path, "*.mp3");
foreach (object File in Files)
{
string filePath = (string) File;
filePath = filePath.Replace("e:\\共享", "").Replace("\\", "/").Replace("'", "''");
System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection("server=(local)\\NetSDK;database=Veimax;Trusted_Connection=yes");
string myString = "INSERT INTO audio (path) VALUES ('" + filePath + "')";
SqlCommand myCommand = new SqlCommand(myString, myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
}
getFiles(Path);
}
}
}
}