==========================================================================================

C#

==========================================================================================


<C#>



-visual sutudio


-1-









-2-











=다른  pc DB 연결 설정

http://gpgstudy.com/gpgiki/MySQL%EC%97%90%EC%84%9C%20%EC%82%AC%EC%9A%A9%EC%9E%90%EC%99%80%20%EB%8D%B0%EC%9D%B4%ED%84%B0%EB%B2%A0%EC%9D%B4%EC%8A%A4%20%EB%A7%8C%EB%93%A4%EA%B8%B0


mysql> grant all privileges on *.* to root@'%' identified by 'root' with grant o

ption;

Query OK, 0 rows affected (0.03 sec)


mysql> select host, user, password from user;

+-----------+------+-------------------------------------------+

| host      | user | password                                  |

+-----------+------+-------------------------------------------+

| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

| 127.0.0.1 | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

| ::1       | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

| %         | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

+-----------+------+-------------------------------------------+

4 rows in set (0.00 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)



=DB <-> CONNECTION <-> DATAADPTER <->DATASET <-> WINFORM

가상 테이블














= F11 -> 코드 라인별 진행

http://itsmart333.tistory.com/attachment/cfile10.uf@233C9E33558A08A81AFFD5.pdf


=실습

-MySQL 연동


-DB생성

create database mook;

use mook;

create table CarInfo(M_Num int Not Null auto_incrememt Primary key,

M_Name varchar(10) Not Null,

M_Year int Not Null,

M_Price varchar(20) Not Null,

M_Door int Not Null);



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;

        
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 Control_Clear() // 입력란 공백 처리
        {
            
this.txtName.Clear();
            
this.txtYear.Clear();
            
this.txtPrice.Clear();
            
this.txtDoor.Clear();
        }

        
private void btnSave_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 
= "insert into CarInfo(M_Name, M_Year, M_Price, M_Door)";
                    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);
                }
            }            
        }

        
private void lvList_Click(object sender, EventArgs e)
        {
            
            
this.txtName.Text = this.lvList.SelectedItems[0].SubItems[1].Text;
            
this.txtYear.Text = this.lvList.SelectedItems[0].SubItems[2].Text;
            
this.txtPrice.Text = this.lvList.SelectedItems[0].SubItems[3].Text;
            
this.txtDoor.Text = this.lvList.SelectedItems[0].SubItems[4].Text;
            
this.Data_Num = this.lvList.SelectedItems[0].SubItems[0].Text;
  

        }
    }
}







==========================================================================================

C++

==========================================================================================


<C++>


=특수화(특정 타입만 다른 로직 적용)


#include <iostream>
#include <string.h>
using namespace std;

template <class T>
T add(T a, T b)
{
    
return a+b;
}

template <> char * add<char *>(char * a, char * b)
{
    
char* c = new char[strlen(a) + strlen(b) +1];
    strcpy(c,a);
    strcat(c,b);
    
return c;
}


int main()
{

    cout 
<< add(1,2<< endl;
    cout 
<< add((char *)"hi ",(char *)"c++"<< endl;
    
return 0;

}






=클래스 템플릿



#include <iostream>
using namespace std;
template <typename T>
class smart
{
    
public:
        T Num;
};


int main()
{
    smart obj;
    obj.Num 
= 1;
    cout 
<< obj.Num << endl;
    
return 0;
}








=> 오류 수정 <객체 생성시 <type> 명시


#include <iostream>
using namespace std;
template <typename T>
class smart
{
    
public:
        T Num;
};

int main()
{
   
 smart<int> obj;
    obj.Num 
= 1;
    cout 
<< obj.Num << endl;
    
return 0;
}







= 함수 클래스 외부 선언시 템플릿


#include <iostream>
using namespace std;
template <typename T>
class smart
{
    
public:
        T Num;
        T test(T a);
       
// {
       //     Num = a;
       //     return Num;
       // }
};


template <typename T>
T smart<T>::test(T a)
{
    Num 
= a;
    
return Num;
}


int main()
{
    smart
<int> obj;
    obj.Num 
= 1;
    cout 
<< obj.Num << endl;
    cout 
<< obj.test(3<< endl;
    
return 0;
}






=템플릿은 구현까지 헤더에 다 넣어야 함(분할 컴파일시)




'2015 스마트 콘트롤러 > 업무일지' 카테고리의 다른 글

20150626  (0) 2015.06.27
20150625  (0) 2015.06.26
20150623  (0) 2015.06.23
20150622  (0) 2015.06.22
20150619  (0) 2015.06.21
Posted by ahj333
,