将文件路径划分为多个数据库列

本文关键字:数据库 文件 路径 划分 | 更新日期: 2023-09-27 18:10:48

我正在尝试划分文件路径并将其填充到多个数据库列中。

因此,如果字符串是C:'Engineering'Structural'CAD'Baghouse.dwg,那么它将填充8个数据库列,5个包含值,3个包含"。

DIR01   |   C:  
DIR02   |   Engineering  
DIR03   |   Structural  
DIR04   |   CAD  
DIR05   |   Baghouse.dwg  
DIR06   |     
DIR07   |     
DIR08   |     

我可以使用Path.DirectorySeparatorChar轻松地分隔文件路径,当我调试并查看Locals框时,数组看起来很完美。

我不明白的是如何访问数组的每个元素并将它们放入单独的列中。

    private void cmdDelimitFilePath_Click(object sender, EventArgs e)
    {
        string SqlCmd;
        string ScannedPath = String.Empty;
        string DIR01 = String.Empty;
        string DIR02 = String.Empty;
        string DIR03 = String.Empty;
        string DIR04 = String.Empty;
        string DIR05 = String.Empty;
        string DIR06 = String.Empty;
        string DIR07 = String.Empty;
        string DIR08 = String.Empty;
        DataTable dt = new DataTable("DirectoryAnalysis");
        SqlConnectionStringBuilder ConnStrBuilder = new SqlConnectionStringBuilder();
        try
        {
            ConnStrBuilder.DataSource = txtServer.Text;
            ConnStrBuilder.InitialCatalog = txtSourceSchema.Text;
            ConnStrBuilder.Password = txtPassword.Text;
            ConnStrBuilder.UserID = txtUser.Text;
            //this connects to the database and creates the new fields
            using (DbConnection connexx = new SqlConnection(ConnStrBuilder.ConnectionString))
            {
                connexx.Open();
                using (DbCommand command = new SqlCommand("ALTER TABLE [DirectoryAnalysis] ADD [DIR01] varchar(100), [DIR02] varchar(100), [DIR03] varchar(100), [DIR04] varchar(100), [DIR05] varchar(100), [DIR06] varchar(100), [DIR07] varchar(100), [DIR08] varchar(100)"))
                {
                    command.Connection = connexx;
                    command.ExecuteNonQuery();
                }
            }
            // this connects to the database and populates the new fields
            using (SqlConnection Conn = new SqlConnection(ConnStrBuilder.ConnectionString))
            {
                Conn.Open();
                SqlCmd = "SELECT [DA_Id], [ScannedPath], [DIR01], [DIR02], [DIR03], [DIR04], [DIR05], [DIR06], [DIR07], [DIR08] FROM [DirectoryAnalysis]";
                using (SqlDataAdapter da = new SqlDataAdapter(SqlCmd, Conn))
                {
                    da.Fill(dt);
                    foreach (DataRow dr in dt.Rows)
                    {
                        ScannedPath = Convert.ToString(dr["ScannedPath"]);
                        //This returns each individual folder in the directories array.
                        string[] directories = ScannedPath.Split(Path.DirectorySeparatorChar);
                        //You can get the number of folders returned like this:
                        int folderCount = directories.Length;
                        // everything works perfectly up to here... 
                        foreach (string part in directories)
                        {
                            // how to access elements of the array?
                            //this is as close as I have been...
                            DIR01 = Convert.ToString(part[0]);
                            dr["DIR01"] = DIR01;
                            DIR02 = Convert.ToString(part[1]);
                            dr["DIR02"] = DIR02;
                            DIR03 = Convert.ToString(part[2]);
                            dr["DIR03"] = DIR03;
                            // and repeat through 8 if this would work
                        }
                    }
                    MessageBox.Show("DirectoryAnalysis has been updated.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        finally
        {
            this.Cursor = Cursors.Default;
        }
    }

将文件路径划分为多个数据库列

如果我理解正确,问题如下:

你需要同时访问数组"directories"中的所有元素。然而,如果你这样做,你就失去了它:

foreach(目录中的字符串部分)

,因为"part"是当前元素,很难取前n个元素。

因此,我认为修复方法是:

停止使用foreach循环,并像这样访问数组的每个元素:

dir1 = directories[0]Dir2 = directories[1]

等等

像这样,你也可以直接在sql insert语句中使用它们。

希望这对你有帮助!

这样怎么样:

string[] StrArr = filePath.Split(''');

.

for (int i = 0; i < StrArr.length - 1; i++)
{
    //Run this SQL command:
    String.Format("UPDATE [table] (DIR{0}) VALUES ({1})", i + 1, StrArr[i])
}
  1. 将字符串拆分为数组
  2. 用for循环循环数组
  3. 的值更新数据库