1. MySQL Connector 설치
2. 테이블 구성
Sqlyog 프로그램을 사용하여 테이블을 만들었습니다.
3. MySQL 사용
콘솔 프로그램을 만듭니다.
그리고 MySql.Data.DLL 을 추가합니다.
MySql.Data.DLL 위치는 설치하신 곳이나 zip 파일 압축 푸신 곳에 있습니다.
-Program.cs-
<p> using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; namespace MySQL { class Program { static void Main( string [] args) { using (MySqlConnection myConnection = new MySqlConnection( @"Data Source=[DB서버];Database=[DB테이블];User ID=[DB접속ID];Password=[DB접속PW]" )) { // DB 열기 myConnection.Open(); // 데이터 삽입 MySqlCommand insertCommand = new MySqlCommand(); insertCommand.Connection = myConnection; insertCommand.CommandText = "INSERT INTO test(name, sub) VALUES(@name, @sub)" ; insertCommand.Parameters.Add( "@name" , MySqlDbType.VarChar, 10); insertCommand.Parameters.Add( "@sub" , MySqlDbType.VarChar, 20); insertCommand.Parameters[0].Value = "정강원" ; insertCommand.Parameters[1].Value = "무직" ; insertCommand.ExecuteNonQuery(); // 데이터 업데이트 MySqlCommand updateCommand = new MySqlCommand(); updateCommand.Connection = myConnection; updateCommand.CommandText = "UPDATE test SET sub=@sub WHERE name=@name" ; updateCommand.Parameters.Add( "@sub" , MySqlDbType.VarChar, 20); updateCommand.Parameters.Add( "@name" , MySqlDbType.VarChar, 10); updateCommand.Parameters[0].Value = "직장인" ; updateCommand.Parameters[1].Value = "정강원" ; updateCommand.ExecuteNonQuery(); // 데이터 검색 MySqlCommand selectCommand = new MySqlCommand(); selectCommand.Connection = myConnection; selectCommand.CommandText = "SELECT * FROM test" ; DataSet ds = new DataSet(); MySqlDataAdapter da = new MySqlDataAdapter( "SELECT *FROM test" , myConnection); da.Fill(ds); foreach (DataRow row in ds.Tables[0].Rows) { Console.WriteLine( string .Format( "이름 : {0} \n비고 : {1}" , row[ "name" ], row[ "sub" ])); } // 데이터 삭제 MySqlCommand deleteCommand = new MySqlCommand(); deleteCommand.Connection = myConnection; deleteCommand.CommandText = "DELETE FROM test WHERE name=@name" ; deleteCommand.Parameters.Add( "@name" , MySqlDbType.VarChar, 10); deleteCommand.Parameters[0].Value = "정강원" ; deleteCommand.ExecuteNonQuery(); // DB 닫기 myConnection.Close(); } } } } </p> |
'프로그래밍 > .NET' 카테고리의 다른 글
[C#]AutoResetEvent, ManualResetEvent (0) | 2013.02.05 |
---|---|
[ASP.NET]ASP.NET에서 WebBrowser(윈폼) 사용하기 (0) | 2013.02.04 |
[C#] HTTP/HTTPS 송수신 (HttpWebRequest/HttpWebResponse) (2) | 2013.01.18 |
현재 포커스 컨트롤러 찾기 (0) | 2012.12.27 |
ToolStriptButton Image Size(이미지 사이즈) 원본 크기로 들고오기 (0) | 2012.12.21 |