예시를 보자
MySqlConnection 내가 만든거
static public MySqlConnection connectDB()
{
MySqlConnection sqlConnection_TEAMDB = new MySqlConnection(비밀);
try
{
sqlConnection_TEAMDB.Open();
return sqlConnection_TEAMDB;
}
catch (Exception e)
{
Log.writeLog(e.ToString());
return null;
}
}
// 1. 값을 읽어오는 예시
using (MySqlConnection connection = ConnectDB.connectDB())
{
using (MySqlCommand command = connection.CreateCommand())
{
command.CommandText = "Select name from item_group";
using (MySqlDataReader reader = command.ExecuteReader())
{
try
{
while (reader.Read())
{
itemGroupBox.Items.Add(reader[0].ToString());
}
}
catch(Exception ex)
{
Log.writeLog(ex.ToString());
}
}
}
}
..2. 값을 업데이트 하는 예시
using (MySqlConnection connection = ConnectDB.connectDB())
{
using (MySqlCommand command = connection.CreateCommand())
{
command.CommandText = "update part " +
"set barcode = @barcodetext " +
"where name = @parttext";
command.Parameters.Add("@barcodetext", MySqlDbType.VarChar).Value = barcodetext;
command.Parameters.Add("@parttext", MySqlDbType.VarChar).Value = parttext;
try
{
int affected = command.ExecuteNonQuery();
if (affected != 0 )
{
MessageBox.Show("저장됐습니다.");
}
else
{
MessageBox.Show("저장에 실패했습니다.");
}
}
catch (Exception ex)
{
Log.writeLog(ex.ToString());
}
}
}
}