NuGet – MySqlConnector 설치
방법1. 포트연결은 : 아니고 ,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using System.Data; using MySql.Data.MySqlClient; static string connectionString = "server=localhost,3306;user id=testuser;database=testdb;Pwd=password;allowuservariables=True"; public static string SelectALL() { string US_ID = ""; MySqlConnection conn = new MySqlConnection(userconnectionString); try { string DOC_HIS = ""; conn.Open(); MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM mc Limit 1;"; MySqlDataAdapter adap = new MySqlDataAdapter(cmd); DataSet ds = new DataSet(); MySqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { US_ID = reader["US_ID"].ToString(); } reader.Close(); return US_ID; } catch (Exception ex) { //클라이언트에게 예외를 던진다. ///throw ex; string Err = ex.ToString(); return US_ID; } finally { if(conn.State == ConnectionState.Open) { conn.Close(); } } } |
Insert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
using MySqlConnector; using System.Data; :::중략 :: static void Main(string[] arges) { var connString = "Server=111.222.333.111,3306;User ID=admin;Password=1223231;Database=Code1"; MySqlConnection conn = new MySqlConnection(connString); var sSQLText = "INSERT INTO Code1.LOG_FILE " + "(SERVERTYPE," + "CCP," + "SERVICE," + "PATH," + "FILENAME)" + " VALUES" + "(@SERVERTYPE," + "@CCP," + "@SERVICE," + "@PATH," + "@FILENAME)"; try { conn.Open(); var cmd = new MySqlCommand(); //using (var cmd = new MySqlCommand()) // { cmd.Connection = conn; cmd.CommandText = sSQLText; cmd.Parameters.AddWithValue("@SERVERTYPE", 0); cmd.Parameters.AddWithValue("@CCP", "AAAA"); cmd.Parameters.AddWithValue("@SERVICE", "LOWFARE"); cmd.Parameters.AddWithValue("@PATH", "FileName"); cmd.Parameters.AddWithValue("@FILENAME", "Hello world"); cmd.ExecuteNonQuery(); //} } catch(Exception ex) { Console.WriteLine(ex.Message.ToString()); } if (conn.State == ConnectionState.Open) { conn.Close(); } Console.WriteLine("ok"); Console.ReadLine(); } |
방법2.
Asp.net Web.config
1 2 3 4 5 6 7 8 9 10 11 12 |
<configuration> <connectionStrings> <!-- MySqlConnection --> <add name="REG_RULE" connectionString="Server=YOURSERVER;User ID=YOURUSERID;Password=YOURPASSWORD;Database=YOURDATABASE" /> </connectionStrings> :: 중략 :: </configuration> |
Core
1 2 3 4 5 6 7 8 |
{ .... "ConnectionStrings": { "Default": "Server=YOURSERVER;User ID=YOURUSERID;Password=YOURPASSWORD;Database=YOURDATABASE" } } |
참조 : https://mysqlconnector.net/tutorials/connect-to-mysql/
Error :
1 2 3 4 |
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''TYPE','CCC','PATH','FILENAME') VALUES(0,'LOWFARE','FileName','He' at line 1 ok |
원인 : MysqlBench 에서 나오는 쿼리에서 ‘(싱글쿼테이션)을 다 빼야지 된다. / C# 하고 java 의 문법적 차이
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
INSERT INTO `BasicCode`.`LOG_FILE` ('TYPE`, `CC`, `SERVICE`, `PATH`, `FILENAME`, `REGI_DATE`) VALUES (<{TYPE: }>, <{CC: }>, <{SERVICE: }>, <{PATH: }>, <{FILENAME: }>, <{REGI_DATE: CURRENT_TIMESTAMP}>); |