반응형
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에 채워 넣음
}
}
}
}
현재 한거 !!
반응형
'Linux' 카테고리의 다른 글
Ubuntu에 React-Native-App/ Expo 환경 세팅 및 개발 방법 (0) | 2023.03.08 |
---|---|
라즈베리파이 -리눅스에 HeidiSQL 설치하기 ! (0) | 2023.02.23 |
SMART FACTORY - 라즈베리파이에 온도 센서를 부착시켜보았다. /파이썬 -MySql DB연결 ! (0) | 2020.09.13 |
SMART FACTORY - 뷰 함수 / HTML/ FLASK 웹서버 구축하기 (0) | 2020.07.28 |
SMART FACTORY - PYTHON 을 조금 더 공부 해 볼깝쏘? / 오늘 수업 뒤죽 박죽이네 (0) | 2020.07.24 |
리눅스 GIT CLONE /PYTHON 공부 /C# 델리게이트 (0) | 2020.07.23 |