在写入数据库之前解析表并添加aditional值 [英] Parse a Table, and Add aditional value, before writing to Database
本文介绍了在写入数据库之前解析表并添加aditional值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从Html表中解析表,它有4列,然后我使用Sqlbulkcopy将其写入数据库。
i需要能够添加批量上传之前从文本框到数组/列表的值。这是我可以根据数据库中的机器名称来引用的吗?
I parse table from a Html table, that has 4 columns, and then i write that into a Database, using Sqlbulkcopy.
i need to be able to add a value from a textbox to the array/list before i bulk upload. This is so i can reference based on a machine name from a database ?
string Url = "http://" + TextBox1.Text + "/logs/Logfile.html";
HtmlAgilityPack.HtmlWeb WebPage = new HtmlAgilityPack.HtmlWeb();
HtmlAgilityPack.HtmlDocument document = WebPage.Load(Url);
var table = new DataTable("Sample Table");
table.Columns.Add("Entry", typeof(string));
table.Columns.Add("Date", typeof(string));
table.Columns.Add("Time", typeof(string));
table.Columns.Add("Description", typeof(string));
table.Columns.Add("Server", typeof(string));
var nodes = document.DocumentNode.SelectNodes("//table[7]/tr[position() > 1]");
nodes
.Skip(1) // skips for headers
.Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim())
.ToArray())
.ToList()
.ForEach(row => table.Rows.Add(row));
using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
using (SqlBulkCopy copy = new SqlBulkCopy(conn))
{
copy.DestinationTableName = "LogEntries";
copy.WriteToServer(table);
conn.Close();
}
}
推荐答案
目前还不清楚你想写什么,所以我只做了一个例子:
it's not clear what exactly you want to write, so I made just an example:
table.Rows.Add("entry", "date", "time", textBox1.Text, "server");
我认为你的意思就像这个例子。我不习惯像对节点对象一样添加所有方法。也许还有一种方法可以将服务器添加到列表中,但我会这样做:
I think you mean like this example. I'm not used to adding all methods behind eachother as you do with the nodes object. Maybe there is also a way to add the server to the list, but I would do it this way:
string Url = "http://" + TextBox1.Text + "/logs/Logfile.html";
HtmlAgilityPack.HtmlWeb WebPage = new HtmlAgilityPack.HtmlWeb();
HtmlAgilityPack.HtmlDocument document = WebPage.Load(Url);
var table = new DataTable("Sample Table");
table.Columns.Add("Entry", typeof(string));
table.Columns.Add("Date", typeof(string));
table.Columns.Add("Time", typeof(string));
table.Columns.Add("Description", typeof(string));
table.Columns.Add("Server", typeof(string));
var nodes = document.DocumentNode.SelectNodes("//table[7]/tr[position() > 1]");
nodes
.Skip(1) // skips for headers
.Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim())
.ToArray())
.ToList()
.ForEach(row => table.Rows.Add(row));
for(DataRow row in table.Rows)
row["Server"] = TextBox1.Text;
using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
conn.Open();
using (SqlBulkCopy copy = new SqlBulkCopy(conn))
{
copy.DestinationTableName = "LogEntries";
copy.WriteToServer(table);
conn.Close();
}
}
nodes
.Skip(1) // skips for headers
.Select(tr => tr.Elements("td").Select(td => td.InnerText.Trim())
.ToArray())
.ToList()
.ForEach(row => table.Rows.Add(row));
DataColumn Servcol = new DataColumn("CloudServer", typeof(String));
table.Columns.Add("Servcol");
foreach (DataRow row in table.Rows)
{
row["servcol"] = txt_Server.Text;
}
就像一个魅力......
$ b输入$ b,让我走向正确的方向
works Like a Charm....
for the Input, Put me in the Right direction
这篇关于在写入数据库之前解析表并添加aditional值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文