将文件文件数据从目录或文件夹检索到sql中的特定数据库字段

时间:2013-03-19 05:50:18

标签: c# sql-server winforms

我想从目录中检索文本文件数据到sql中的特定数据库字段。这是一个代码:

try
{
    FolderBrowserDialog fBrowser = new FolderBrowserDialog();
    //create instance of folder browser to navigate to desired folder to compress files
    DialogResult result = fBrowser.ShowDialog();
    //process this if user clicks OK button
    if (result == DialogResult.OK)
    {
       //string strPath stores chosen path
       strPath = fBrowser.SelectedPath;
       //put that path in the textbox1
       txtSource.Text = strPath;
    }
    //set current directory to be the one you navigated to 
    //(this is also the folder that will store the compressed file)
    Directory.SetCurrentDirectory(strPath);
    //get contents of directory stored in "strPath"
    DirectoryInfo di = new DirectoryInfo(strPath);
    //create array that holds requested files from folder stored in "di" variable
    DirectoryInfo[] rgFiles = di.GetDirectories("*.*");
    //move through DirectoryInfo array and store in new array of fi
    foreach (DirectoryInfo fi in rgFiles)
    {
       checkedListBox1.Items.Add(fi.Name); //add folders located into listbox
    }
}

这里的目录包含no。对于图像和txt文件,如果我们选择checklistbox,则应将txt文件中的所有数据添加到与其数据类型相关的特定sql数据库中。 任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

要检查目录是否包含文件,可以使用名为“Directory”的System.IO类及其方法“GetFiles”。您需要做的就是传入目录的路径。它将返回该目录中文件的String Array。详情请见:

http://msdn.microsoft.com/en-us/library/07wt70x2.aspx

最好的方法是在数据库中创建一个可以处理传入请求的存储过程,并将数据插入到正确的表中。以下是Sql-Server存储过程的介绍:

http://msdn.microsoft.com/en-us/library/aa174792(v=sql.80).aspx

然后,您需要在C#代码中创建与数据库的连接:

http://msdn.microsoft.com/en-us/library/s4yys16a(v=vs.71).aspx

最后,您需要使用刚创建的连接调用存储过程并传递C#代码中的参数,这些参数已在StackOverflow上多次回答:

How to execute a stored procedure within C# program