================================ Outline ====================================
MySql 데이터 연동
----------------------------------------------------------------------------











MySql 연동 pdf.
2.MySql연동.pdf


//가상 데이터란 런타임에서만 메모리 상에 구현되는 데이터를 말한다.






/*** 소스 ***/
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 MySql.Data.MySqlClient;
namespace mook_Car {
public partial class Form1 : Form { private string StrSQL = "Data Source =localhost;Database=mook;User Id=root;Password=root"; //데이터베이스 연결 문자열 private string Data_Num; //선택된 컨트롤 행의 값 저장
void Control_Clear() // 입력란 공백 처리 {
this.txtName.Clear(); this.txtDoor.Clear(); this.txtPrice.Clear(); this.txtYear.Clear(); }
public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { lvList_MySqlClient_View(); }
private void lvList_MySqlClient_View() { this.lvList.Items.Clear(); var MConn = new MySqlConnection(StrSQL); MConn.Open(); var Comm = new MySqlCommand("Select * from CarInfo", MConn); var MyRead = Comm.ExecuteReader();
while(MyRead.Read()) { var strArray = new String[] {MyRead["M_Num"].ToString(), MyRead["M_Name"].ToString(), MyRead["M_Year"].ToString(), MyRead["M_Price"].ToString(), MyRead["M_Door"].ToString()}; var lvt = new ListViewItem(strArray); this.lvList.Items.Add(lvt); } MyRead.Close(); }
private void stnSave_Click(object sender, EventArgs e) { if(this.txtName.Text != ""&& this.txtYear.Text != "" && this.txtPrice.Text != "" && this.txtDoor.Text != "") { var Conn = new MySqlConnection(StrSQL); Conn.Open(); string Sql = "values('" + this.txtName.Text +"'," + this.txtYear.Text + ",'" + this.txtPrice.Text + "'," + this.txtDoor.Text + ")"; var Comm = new MySqlCommand(Sql, Conn); int i = Comm.ExecuteNonQuery(); Conn.Close(); if(i == 1) { MessageBox.Show("정상적으로 데이터가 저장되었습니다.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Information); lvList_MySqlClient_View(); Control_Clear(); } else { MessageBox.Show("정상적으로 데이터가 저장되지 않았습니다.", "에러", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void btnModify_Click(object sender, EventArgs e) { if(this.txtName.Text != ""&& this.txtYear.Text != "" && this.txtPrice.Text != "" && this.txtDoor.Text != "") { try { var Conn = new MySqlConnection(StrSQL); Conn.Open(); var MySqlAdapter = new MySqlDataAdapter( "select * from CarInfo", Conn); var ds = new DataSet(); MySqlAdapter.Fill(ds, "dsTable"); var dt = ds.Tables["dsTable"].Select("M_Num =" + Convert.ToInt32(this.Data_Num), null, DataViewRowState.CurrentRows); DataRow drTemp; drTemp = dt[0]; drTemp["M_Name"] = this.txtName.Text; drTemp["M_Year"] = this.txtYear.Text; drTemp["M_Price"] = this.txtPrice.Text; drTemp["M_Door"] = this.txtDoor.Text; var cmdBuild = new MySqlCommandBuilder(MySqlAdapter); MySqlAdapter.UpdateCommand = cmdBuild.GetUpdateCommand(); MySqlAdapter.Update(ds, "dsTable"); cmdBuild.Dispose(); Conn.Close(); MessageBox.Show("정상적으로 데이터가 수정되었습니다.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Information); lvList_MySqlClient_View(); Control_Clear(); } catch { MessageBox.Show("정상적으로 데이터가 수정되지 않았습니다.", "에러", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
} }
|