c#에서 Mariadb를 연결할때 MySqlConnector를 사용한다.
VS에서 NgGet을 이용해서 쉽게 검색하고 설치할 수 있다.
*NgGet 이놈 참 좋다~
MySqlConnector는 MIT 라이센스 그냥 사용하면 된다.
connector설치한김에 테스트까지 해봐야지요
c#코드
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;
namespace ScrappingWinForms
{
public partial class Form1 : Form
{
string sConnectionString = "Server=localhost;Uid=root;Pwd=???????;Database=???";
String Url = string.Empty;
public Form1()
{
InitializeComponent();
//WebBrowser로 사이트 로딩시 자바스크립트 바인딩안되어 에러나는 경우.
//자바스크립트 오류(dialogbox) 무시하고 진행가능하도록 제어함.
this.webBrowserViewer.ScriptErrorsSuppressed = true;
webBrowserViewer.DocumentTitleChanged += new EventHandler(webBrowserViewer_DocumentTitleChanged);
}
private void btnSearch_Click(object sender, EventArgs e)
{
surftheWeb();
}
private void surftheWeb()
{
Url = txtUrl.Text;
webBrowserViewer.Navigate(Url);
}
/// <summary>
/// WebBrowser's DocumentCompleted Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void webBrowserViewer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// ((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
WebBrowser oBrowser = sender as WebBrowser;
txtSource.Text = StreamConvertEUCKRtoUTF8(oBrowser);
}
/// <summary>
/// Using WebBrowser web page
/// 웹페이지 Encoding type 이 EUC-KR 이면 WebBrowser.DocumentText로 html로 렌더링된 text를 볼때 한글이 깨진다.
/// 아래처럼 스트림리더로 페이지 EUC-KR type을 읽어들인다.
/// </summary>
/// <param name="oBrowser"></param>
/// <returns></returns>
private String StreamConvertEUCKRtoUTF8(WebBrowser oBrowser)
{
String sResult = String.Empty;
Stream oStream;
StreamReader oStreamReader;
oStream = oBrowser.DocumentStream;
oStreamReader = new StreamReader(oStream, System.Text.Encoding.GetEncoding(oBrowser.Document.Encoding));
oStream.Position = 0;
sResult = oStreamReader.ReadToEnd();
return sResult;
}
private void webBrowserViewer_DocumentTitleChanged(object sender, EventArgs e)
{
//this.Text = webBrowser1.DocumentTitle.ToString();
this.groupBox1.Text = webBrowserViewer.DocumentTitle;
}
private void loadTestData() {
MySqlConnection oConnection = new MySqlConnection(sConnectionString);
oConnection.Open();
try
{
MySqlCommand cmd = oConnection.CreateCommand();
cmd.CommandText = "SELECT * FROM TMP_help order by help_topic_id desc";
MySqlDataAdapter aDap = new MySqlDataAdapter(cmd);
DataSet dsData = new DataSet();
aDap.Fill(dsData);
dgViewSample.DataSource = dsData.Tables[0].DefaultView;
}
catch (Exception ex)
{
throw ex;
}
finally {
if (oConnection.State == ConnectionState.Open)
{
oConnection.Close();
}
}
}
private void btnForTest_Click(object sender, EventArgs e)
{
loadTestData();
}
}
}
'프로그래밍 > c#' 카테고리의 다른 글
c#에서 mariadb사용하기 (0) | 2018.11.10 |
---|---|
WebBrowser이용한 스크린스크래핑 (0) | 2018.05.09 |
WebBrowser.DocumentText 한글깨짐 (0) | 2018.05.07 |