================================ Outline ====================================
ADO.NET 복습
MySql과 자료 연동하여 학생 명부 만들기
----------------------------------------------------------------------------
ADO.NET 복습


데이터베이스 소프트 웨어(MySql, MsSql, Access 등)에서 자료를 저장할 때 XML 형식을 제공한다.












DataReader는 데이터와 지속적인 연결이 필요하지만 DB를 읽어오는 시간이 빠르다. 반면에 DataAdapter는 DB가 필요할때만 연결함으로 시스템 자원 낭비를 최소화하지만 DB를 읽어오는 시간은 DataReader에 비해 느리다.
DB에서 XML로 뽑아주는 것이 DataAdapter이다.
MySql과 자료 연동하여 학생 명부 만들기(진행중)
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; using MySql.Data;
namespace _20150626 { public partial class Form1 : Form { private string StrSQL = "Data Source=localhost;Database=test;User id=root;Password=root"; //user id=root;persistsecurityinfo=True;server=localhost;database=test public Form1() { InitializeComponent(); }
void Control_Clear() // 입력란 공백 처리 {
this.tb_Name.Clear(); this.tb_Email.Clear(); this.tb_Phone.Clear(); this.tb_Address.Clear(); this.tb_Company.Clear(); this.tb_Department.Clear(); this.tb_Introduce.Clear(); }
private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'testDataSet.myfriend' table. You can move, or remove it, as needed. this.myfriendTableAdapter.Fill(this.testDataSet.myfriend); } private void lvList_MySqlClient_View() { this.lvList.Items.Clear(); var MConn = new MySqlConnection(StrSQL); MConn.Open(); var Comm = new MySqlCommand("Select * from myfriend", MConn); var MyRead = Comm.ExecuteReader();
while (MyRead.Read()) { var strArray = new String[] {MyRead[0].ToString(), MyRead[1].ToString(), MyRead[2].ToString(), MyRead[3].ToString(), MyRead[4].ToString(), MyRead[5].ToString(), MyRead[6].ToString(), MyRead[7].ToString(), MyRead[8].ToString()}; var lvt = new ListViewItem(strArray); this.lvList.Items.Add(lvt); } MyRead.Close(); }
private void btn_Add_Click(object sender, EventArgs e) { string name = tb_Name.Text.Trim(); string email = tb_Email.Text.Trim(); string phone = tb_Phone.Text.Trim(); string mobile = tb_Mobile.Text.Trim(); string address = tb_Address.Text.Trim(); string company = tb_Company.Text.Trim(); string department = tb_Department.Text.Trim(); string introduce = tb_Introduce.Text.Trim();
var conn = new MySqlConnection(StrSQL); conn.Open(); String sql = "INSERT INTO VALUES('" + this.tb_Name.Text + "," + this.tb_Email.Text + ',' + this.tb_Phone.Text + ',' + this.tb_Mobile.Text + ',' + this.tb_Address.Text + ',' + this.tb_Company.Text + ',' + this.tb_Department.Text + ',' + this.tb_Introduce.Text + ")"; var Comm = new MySqlCommand(sql, conn); int i = Comm.ExecuteNonQuery();
if (i == 1) { MessageBox.Show("정상적으로 데이터가 저장되었습니다.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Information); Control_Clear(); lvList_MySqlClient_View(); } else { MessageBox.Show("정상적으로 데이터가 저장되지 않았습니다.", "에러", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/*private void btn_Modify_Click(object sender, EventArgs e) { var conn = new MySqlConnection(StrSQL); conn.Open(); var MySqlAdapter = new MySqlDataAdapter( "select * from myfriend", conn); var ds = new DataSet(); MySqlAdapter.Fill(ds, "dsTable"); var dt = ds.Tables["dsTable"].Select("tb_Name =" + Convert.ToInt32(this.Date_Num), null, DataViewRowState.CurrentRows);
}*/ }
}
|
