본문 바로가기

Programming/MySQL

C# 으로 같은 네트워크 간 SQL 연동

프롬프트창 IP4 확인. - pc -              192.168.10.88        - Server

                                notebook -     192.168.10.99       - Client

 

(

둘다 사설, 동적아이피였음.  고정 or 고정 ip일때 어떨지 test 필요

포트포워딩 했지만 필요한지는 모르겠음

노트북은 본래 와이파이사용, 셋째자리가 0이었지만 같은 라우터 사용해본 뒤 성공, test 필요.

)

 

Server 측

(

mysql 포트 3306, 사용자이름 걍 root 로 함, 호스트주소 걍 local 호스트 해도 상관없음 

)

 

대충 db, 테이블 만듬.

 

Mysql  Command line shell 실행

 

그냥 프롬프트 창에서 실행 시

mysql - Mysql server 5.x - bin - mysql -h (ip주소) -p (패스워드)       치면 됨.

 

grant all privileges on *.* to '계정'@'ip' identified by '비번' 

        /////        로 해당 ip 의 접속을 허용할 수 있음.  ip대신 '%' 넣어주면 전체허용임

 

* 셋팅 다하고 클라이언트에서 mysql 서버로 error 1040 떴을때

 

grant all privileges on *.* to '계정'@'ip' identified by '비번' with grant option 쳐주니까 됬음!!!!! 

 

error 1130 떴을때도 이거쳐주면 된다함.

 

아마도 계정 인식에 대한 오류라고 생각됨, 확인 필요

 

 

Client 측

 

C# - Window form 으로 작성함.

 

프로젝트 생성 후 Mysql connection 을 위한 클래스파일 하나 만듬.

 

Mysql.Data 등의 참조는 기본.

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MySql.Data.MySqlClient;

 

namespace _150605_slq
{
    class Class1   //// 이름안바꾸니까 걍 Class1 이라고 만들어짐 
    {
        public static string constring1 = "SERVER=192.168.10.88;Database=sampdb;User Id=root;Password=1234 ";

 

//// constring1 이란 이름의 Connection 위한 쿼리문 작성.  서버ip, db명, 아이디, 패스워드 기재, 참고로 윈폼에 textBox1, Button1, DataGridView1 하나씩 만듬.

 


        MySqlConnection conn = new MySqlConnection();

        private string sConnString = "";

        public void ConnectDB()
        {
            try
            {
                sConnString = constring1;
            }
            catch
            {
            }

            if (conn.State.ToString().Equals("Closed"))
            {
                conn.ConnectionString = sConnString;
                conn.Open();
            }
        }

        public void CloseDB()
        {
            if (conn != null)
            {
                conn.Close();
            }
        }

        public DataTable GetDBTable(string sql)
        {
            MySqlDataAdapter adapter = new MySqlDataAdapter(sql, conn);
            MySqlCommandBuilder builder = new MySqlCommandBuilder(adapter);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;
        }
    }
}

 

윈폼에는

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MySql.Data.MySqlClient;

namespace _150605_slq
{
    public partial class Form1 : Form
    {
        Class1 db;   //// 요고 신기함. 만들어논 클래스파일을 사용한다는것같음.


        public Form1()
        {
            InitializeComponent();
            db = new Class1();
            db.ConnectDB();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

           
       

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "select g2g, g2v, 24r2 from sampdb.inform where g2g =  '" + textBox1.Text + "'";
            DataTable dt = db.GetDBTable(sql);
            dataGridView1.DataSource = dt;

            db.CloseDB();
        }
    }
}
//// 버튼에만 명령넣음.

string sql 추가해서, 또 쿼리문 넣는걸 볼수 있음.

g2g, g2v, 24r2 는 행임. 행을 뭐라 했는대 기억안남.

 

sampdb(db명).inform(table명) 임. g2g 세로줄 의 (textBox1) 에 있는 행 의 g2v, 24r2 를 보여준다는 뜻임. 기입해넣는방법 신기함.

 

데이터그리드뷰 도 신기함. 전체검토후 갱신해야겠음

 

SQLyog 새 연결 한 후 server 측 아이피 적어놓고 (당연히 포트는 3306) 테스트 연결로 테스트함.

이때 에러 1045가 떠서 개삽질하다 오류 해석하고 해결함.

 

이 테스트 연결로 연결테스트 쉽게 할 수 있을것 같음.

 

'Programming > MySQL' 카테고리의 다른 글

[펌]MySQL 기본 쿼리문  (0) 2015.09.24
MySQL 정의/특징/시작  (0) 2015.05.19