【发布时间】:2015-03-24 02:23:47
【问题描述】:
我想将正则表达式匹配中的数据/字符串插入到我的 SQL 数据库中的表中。
这是我使用的代码示例。
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Threading.Tasks;
using HtmlAgilityPack;
using System.Data;
using System.Net;
using System.Text.RegularExpressions;
namespace AutoApp_Win32Server
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("1,0\n\n");
Console.WriteLine("1,0");
HtmlWeb web = new HtmlWeb();
HtmlDocument doc1 = web.Load("http://brandstofprijzen.info/?postcode=&plaats=8801&afstand=25&brandstof=Diesel&zoeken=Zoeken");
HtmlNodeCollection tables = doc1.DocumentNode.SelectNodes("/html/body/center/table");
HtmlNodeCollection rows = tables[13].SelectNodes(".//tr");
string makeSpace = " ";
for (int i = 1; i < rows.Count; ++i)
{
HtmlNodeCollection cols = rows[i].SelectNodes(".//td");
string nawhtml = cols[0].InnerHtml;
string brandstof = cols[1].InnerText;
string prijs = cols[2].InnerText;
string datum = cols[3].InnerText;
Regex match1 = new Regex(@"<b>\s*(.+?)\s*</b><br>");
Match naamtankstation = match1.Match(nawhtml);
Console.WriteLine("Naam : " + naamtankstation.Groups[1].Value);
Regex match2 = new Regex(@"</b><br>\s*(.+?)\s*<br>");
Match straattankstation = match2.Match(nawhtml);
Console.WriteLine("Straat : " + straattankstation.Groups[1].Value);
Regex match3 = new Regex(@"[^\(]*<br>\s*(.+?)\s*<br>");
Match postcodetankstation = match3.Match(nawhtml);
Console.WriteLine("Postcode : " + postcodetankstation.Groups[1].Value);
//Console.WriteLine("naw : " + nawhtml);
Console.WriteLine("Brandstof : " + brandstof);
Console.WriteLine("Prijs : " + prijs);
Console.WriteLine("Datum : " + datum);
Console.WriteLine(makeSpace);
Console.WriteLine(makeSpace);
}
Console.ReadKey();
}
}
}
数据库代码:
using System.Data.SqlClient;
var conn = new SqlDbConnection();
conn.ConnectionString =
"Data Source=ServerName;" +
"Initial Catalog=DataBaseName;" +
"User id=UserName;" +
"Password=Secret;";
conn.Open();
谁能给我一个例子,我如何将 Console.Writeline 字符串发送到 SQL 表。
【问题讨论】:
标签: c# sql sql-server regex string