본문 바로가기

Linux

SMART FACTORY -MYSQL /C# 데이터 베이스 연동

반응형

using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QRcodeDBconn
{
    public partial class Form1 : Form
    {
        string strConnString = "Data Source=127.0.0.1;Initial Catalog=QRCodeDB;Persist Security Info=True;User ID=sa;Password=p@ssw0rd!";
        string mode = "";

        public Form1()
        {
            InitializeComponent();
        }

       
        private void UpdateData()
        {
            using(SqlConnection conn = new SqlConnection(strConnString))
            {
                conn.Open();
                string strQuery = " SELECT ProductCode, ProductName, ProductColor FROM dbo.ProductTbl ";
                SqlDataAdapter dataAdapter = new SqlDataAdapter(strQuery, conn);
                DataSet ds = new DataSet();
                dataAdapter.Fill(ds, "ProductTbl");

                GridProTbl.DataSource = ds;
                GridProTbl.DataMember = "ProductTbl";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            UpdateData();

        }

        private void BtnNew_Click(object sender, EventArgs e)
        {
            TxtProCode.Text = TxtProColor.Text = TxtProName.Text = "";
            mode = "INSERT";
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            SaveProcess();
            UpdateData();
        }

        private void SaveProcess()
        {
            //throw new NotImplementedException();

            if (string.IsNullOrEmpty(mode))
            {
                MessageBox.Show(this, "신규버튼을 누르고 데이터를 저장하세요", "경고",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            using (SqlConnection conn = new SqlConnection(strConnString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                string strQuery = "";

                if (mode == "UPDATE")
                {
                    strQuery = " UPDATE dbo.ProductTbl " +
                               " SET ProductCode = @ProductCode " +
                               " , ProductName = @ProductName " +
                               " , ProductColor = @ProductColor ";
                    cmd.CommandText = strQuery;
                }
                else if (mode == "INSERT")
                {
                    strQuery = " INSERT INTO dbo.ProductTbl " +
                               " (ProductCode, ProductName, ProductColor) " +
                               " VALUES (@ProductCode, @ProductName, @ProductColor)" ;
                    cmd.CommandText = strQuery;
                }

                SqlParameter parmProductCode = new SqlParameter("@ProductCode", SqlDbType.Int);
                parmProductCode.Value = TxtProCode.Text;
                cmd.Parameters.Add(parmProductCode);

                SqlParameter parmProductName = new SqlParameter("@ProductName", SqlDbType.NVarChar, 20);
                parmProductName.Value = TxtProName.Text;
                cmd.Parameters.Add(parmProductName);

                SqlParameter parmProductColor = new SqlParameter("@ProductColor", SqlDbType.NChar, 10);
                parmProductColor.Value = TxtProColor.Text;
                cmd.Parameters.Add(parmProductColor);

                cmd.ExecuteNonQuery();

            }
        }
    }
}

 

 

예전에 한거 참고 !!! 

using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp10
{
    class Program
    {
        static void Main(string[] args)
        {
            DataSet dataSet = new DataSet();
            
            string query = "SERVER=192.168.0.114;DATABASE=mydb ; UID=root; PASSWORD=1234;";
            using (MySqlConnection connection = new MySqlConnection(query))
            {
                connection.Open();
                query = "SELECT * FROM tblRegister";
                MySqlDataAdapter mySqldataAdap = new MySqlDataAdapter(query,connection);
                //아직 데이터를 들고 온게 아니다 ! 
                mySqldataAdap.Fill(dataSet,"tblRegister");
                //디비에서 데이터를 들고와서 dataset에 채워 넣음 


            }
        }
    }
}

 

 

현재 한거 !! 

반응형