我正在尝试分隔文件路径并将其填充到多个数据库列中。
因此,如果字符串是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;
}
}
答案 0 :(得分:1)
如果我理解正确,问题如下:
您需要访问数组的所有元素"目录"同时。但是,你通过这样做来失去它:
foreach(目录中的字符串部分)
因为"部分"是当前的元素,并且采用前面的n个元素是困难的(ish)。
因此,我认为修复是:
停止使用foreach循环并访问数组的每个元素,如下所示:
dir1 =目录[0] dir2 =目录[1]
等等。
像这样,您也可以直接在sql insert语句中使用它们。
希望这有帮助!
答案 1 :(得分:1)
这样的事情:
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])
}