Oct 12, 2015

C# using mysql(database)



This post is using database(My sql).





Above image is testing image.


Displaying datetimepicker's date data.


Default date is today.

1
dateTimePicker1.Value = DateTime.Now;

C# possible to using sync way or async way.

I using async way.

This is database.

schema is test, table is answertest.

There are 4 columns.

id is primary key.

No is number, Record is counter or timer, Date is recording date.

1
2
3
4
5
6
7
8
String strCon = "Server=localhost;Database=test;Uid=test;Pwd=;";
DataSet ds = new DataSet();
string sql = "SELECT * FROM answertest where date like '"+date+"%'";
using(MySqlConnection conn=new MySqlConnection(strCon))
{
      MySqlDataAdapter adpt = new MySqlDataAdapter(sql, conn);
      adpt.Fill(ds, "answertest");
}


I use 3 objects MySqlConnection, MySqlDataAdapter and DataSet

Line 1 : Basic information for connect to mysql.

Line 3 : I use 'like' because db saved date and time, but i use date for query. 

Line 7 : save query data to dataset.

I use foreach.

1
2
3
4
5
6
7
 foreach(DataRow r in ds.Tables[0].Rows)
 {               
    ListViewItem lv = new ListViewItem(r["no"].ToString());
    lv.SubItems.Add(r["record"].ToString());
    lv.SubItems.Add(r["date"].ToString());
    listView2.Items.Add(lv);
 }

Press button '기록'(record), insert db.

1
2
3
4
5
6
7
using (MySqlConnection conn = new MySqlConnection(strCon))
{
    conn.Open();
    MySqlCommand cmd = new MySqlCommand("INSERT INTO answertest (no,record,date) VALUES 
("+recordCnt+",'"+timerText.Text+"','"+dateTimeText.Text.ToString()+"')", conn);
    cmd.ExecuteNonQuery();
}

C# Basic ] MonthCalendar, DateTimePicker



This post is choosing date.

There are two date control.

MonthCalendar and DateTimePicker.

MonthCalendar is Monthly Calendar.

DateTimePicker is date picker.



I added datetimepicker and monthcalendar.

If you click to datetimepick you can see display just one day.

But, MonthCalendar is display max 1 week.


You can see above picture choose day 13~ day 19.

If you want one day, you choose one day.


This is code for monthcalendar.

1
2
3
4
5
6
7
8
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
       if (monthCalendar1.SelectionRange.Start == monthCalendar1.SelectionRange.End)
                textBox1.Text = monthCalendar1.SelectionRange.Start.ToString("yyyy-MM-dd");
            else
                textBox1.Text = monthCalendar1.SelectionRange.Start.ToString("yyyy-MM-dd") + "~" +
 monthCalendar1.SelectionRange.End.ToString("yyyy-MM-dd");
}

MonthCalendar use method 'SelectionRange.Start' and 'SelectionRange.End'.


This is DateTimePicker.


You will click icon, DateTimePicker show pull down calendar.

Your choose date display to left text box.

1
2
3
4
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
      textBox1.Text = dateTimePicker1.Value.ToString("yyyy-MM-dd");
}

datetime picker use method 'value'.


C# Basic ] date time











This post is date time.



I added text box for display datetime.

If press button '기록'(record) adding to list view.

Add new timer for datetime.

1
2
3
4
5
6
7
8
private void timer2_Tick(object sender, EventArgs e)
{
        get_time();
}
private void get_time()
{
        dateTimeText.Text = DateTime.Now.ToString();
}

'timer2_Tick()' call get_time() every 1 second.

'get_time()' is display date time. 

date time format is default 12 times

If you wan't format using DateTime.Now.ToString(your format);

This code is for press button '기록'(record)

1
2
3
4
5
ListViewItem lv = new ListViewItem(recordCnt.ToString());
lv.SubItems.Add(timerText.Text);
lv.SubItems.Add(dateTimeText.Text.ToString());
listView1.Items.Add(lv);
recordCnt++;

Run


every second refresh current date time.

press button '시작'(start) and '기록'(record).



※ P.S

Changing date time format.

1
dateTimeText.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
cs

I changing to format 24-times.


C# Basic ] Listview






I added Listview.

You can see just empty box.

You will add text and listview layout.

There are many way draw layout.

I use simply way.

1
2
3
4
5
6
7
public Form1()
        {
            InitializeComponent();
            listView1.View = View.Details;
            listView1.Columns.Add("No");
            listView1.Columns.Add("Record");
        }

Line 4~6 : Listview column  name.

First item 'No' is id number.

Second item 'Record' is timer or counter.

This is record button for display timer or counter.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
  private void button3_Click(object sender, EventArgs e)
        {
            if(timerCount!=0)
            {
                ListViewItem lv = new ListViewItem(recordCnt.ToString());
                lv.SubItems.Add(timerText.Text);
                listView1.Items.Add(lv);
                recordCnt++;
            }
        }

Line 5 : making 'ListViewItem' and input id information.

Line 6 : input timer of counter information.

Line 7 : display listview.

Line 8 : increase id.

1
listView1.Items.Clear();

This is clear listview.

Run


You can see List view column.

Press '시작'(Start) and '기록'(record) button.


You can see recording timer.

This is counter mode.


This is button '종료'(finish)


List view is clear.

C# Basic ] Checkbox




I changed Radio button to check box.


You can see new layout.



Radio button -> check.

Source code is simple

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked==true)
            {
                timermode = false;
            }
            else
            {
                timermode = true;
            }
            timer_init();
         }

If you checked 'check box' call 'checkBox1_CheckedChanged'

Count mode as 'Checked' is true, Timer mode as 'Checked' is false.

This is protection check box.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
            textBox1.Text = "테스트를 시작합니다.";
            checkBox1.Enabled = false;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            textBox1.Text = "테스트를 종료합니다.";
            checkBox1.Enabled = true;
            timer_init();
        }

Line 5 : deactivate check box.

Line 11 : activate check box.

c# Basic] radio button






I changed layout.


Left is new layout.

I added radio button.

radio button '타이머 모드' is Timer mode.

radio button '카운터 모드'is Counter mode.

Also I added new variable 'timermode'.

1
Boolean timermode = true;

true is '타이머 모드(Timer mode)', false is '카운터 모드(Counter mode)'.


below code is radio button code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (timerCount == 0)
            {
                timermode = true;
                timer_init();
            }
       }
 
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if(timerCount==0)
            {
                timermode = false;
                timer_init();
            }
       }

Line 1~13 is '타이어 모드'(Timer mode), 15~27 is '카운터 모드'(counter mode)


Both '타이머 모드' and '카운터 모드' working as 'timerCout' is '0'.

'timer_init()' is changing setting.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
private void timer_init()
        {
            timerCount = 0;
            if (timermode == true)
            {
                timerText.Text = "00:00:00";
                label2.Text = "타이머 테스트";
            }
            else
            {
                timerText.Text = "0";
                label2.Text = "카운터 테스트";
            }
        }


'timer_display()' is for display information.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
private void timer_display(object sender, EventArgs e)
        {
            timerCount++;
            if(timermode==true)
            {
                int sec = 0, min = 0, hour = 0;
                if (timerCount < 60)
                {
                    sec = timerCount;
                }
                else if (timerCount < 3600)
                {
                    min = timerCount / 60;
                    sec = timerCount % 60;
                }
                else
                {
                    hour = timerCount / 3600;
                    min = (timerCount % 3600) / 60;
                    sec = ((timerCount % 3600) % 60);
                }
               timerText.Text = hour.ToString("00") + ":" + min.ToString("00") + ":" + sec.ToString("00");
            }
            else
            {
                timerText.Text = timerCount.ToString();
            }
          }


Line 4~28 : for timer mode.

Line 29~33 : for counter mode.

Also, radio button deactivation as working timer or counter.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
private void button1_Click(object sender, EventArgs e)
{
      timer1.Start();
      textBox1.Text = "테스트를 시작합니다.";
      radioButton1.Enabled = false;
      radioButton2.Enabled = false;
}
 
private void button2_Click(object sender, EventArgs e)
{
      timer1.Stop();
      textBox1.Text = "테스트를 종료합니다.";
      radioButton1.Enabled = true;
      radioButton2.Enabled = true;
      timer_init();
}

Line 5~6 : Deactivation Radio button when working timer.

Line 13~14 : Activation Radio button when not working timer.

Run~


This is main.


Start timer mode.

Radio button is deactivation.



press '종료'(end) button.

Radio button is activation.




Start counter mode.

Tip!!

Radio button need Group!!

If not include group, radio button not work.