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

C# 将文本文件数据从目录或文件夹检索到sql中的特定数据库字段,c#,sql-server,winforms,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(); //pr

我想从目录中检索文本文件数据到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
    }
}
在这里,目录包含图像和txt文件的数量,如果我们选中复选框,则txt文件中的所有数据都应添加到与其数据类型相关的特定sql数据库中。
任何人都可以帮我检查一下目录是否有文件,您可以使用名为directory的System.IO类及其方法GetFiles。您所需要做的就是传入目录的路径。它将返回该目录中文件的字符串数组。详情可在此找到:

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

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

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


但是首先,如何检查目录是否有文件?我已经更新了我的答案,在答案的开头包含了一个关于directory.GetFiles的部分