I want to insert the data/string from the Regex match, to an table in my SQL database.
Here is the example of the code that I using.
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();
}
}
}
Database Code:
using System.Data.SqlClient;
var conn = new SqlDbConnection();
conn.ConnectionString =
"Data Source=ServerName;" +
"Initial Catalog=DataBaseName;" +
"User id=UserName;" +
"Password=Secret;";
conn.Open();
Can somebody give me a example, how I send the Console.Writeline strings to an SQL table.
Something like this should do the trick
using(SqlConnection connection = new SqlConnection("Data Source=ServerName;" +
"Initial Catalog=DataBaseName;" +
"User id=UserName;" +
"Password=Secret;"))
{
connection.Open();
string sql = "INSERT INTO Address(Naam,Straat,Postcode) VALUES(@param1,@param2,@param3)";
SqlCommand cmd = new SqlCommand(sql,connection);
cmd.Parameters.Add("@param1", SqlDbType.Varchar, 50).value = naamtankstation.Groups[1].Value;
cmd.Parameters.Add("@param2", SqlDbType.Varchar, 50).value = straattankstation.Groups[1].Value;
cmd.Parameters.Add("@param3", SqlDbType.Varchar, 50).value = postcodetankstation.Groups[1].Value;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.