1

私のwinformプロジェクトのアジェンダ機能を作ろうとしています。ユーザーが monthCalendar コントロールで日付を選択したときに、特定の日付のテキスト ボックスにデータベース レコードを表示したいと考えています。以下に、db テーブルのデザイン、winform のデザイン、コード、および取得している例外メッセージを示します。どうすればこれを修正できますか?

*ps: パラメータ化されたクエリの使用について提案する必要はありません。私はできますし、最終的には変更します

ここに画像の説明を入力

ここに画像の説明を入力

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;

namespace EKS
{
    public partial class Agenda : Form
    {
        public Agenda()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            panel1.BackColor = Color.FromArgb(100, 88, 55, 55);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try {

                string myQuery = "insert into agenda (input_agenda, input_date) values ('"+textBox1.Text.ToString()+"', '"+ monthCalendar1.SelectionStart +"')";

                SqlConnection myConn = new SqlConnection();
                myConn.ConnectionString = "Data Source=localhost; database=EKS; uid=sa; pwd=123; connection lifetime=20; connection timeout=25; packet size=1024;";

                SqlCommand myComm = new SqlCommand();
                myComm.Connection = myConn;

                myComm.CommandText = myQuery;

                myConn.Open();
                myComm.ExecuteNonQuery();
                myConn.Close();

                MessageBox.Show("agenda updated");
            }
            catch (Exception x) {
                MessageBox.Show(x.ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try {
                string deleteQuery = "DELETE FROM agenda WHERE input_date = '" + monthCalendar1.SelectionStart +"'";
                SqlConnection myConn = new SqlConnection();
                myConn.ConnectionString = "Data Source=localhost; database=EKS; uid=sa; pwd=123; connection lifetime=20; connection timeout=25; packet size=1024;";

                SqlCommand myComm = new SqlCommand();
                myComm.Connection = myConn;

                myComm.CommandText = deleteQuery;

                myConn.Open();
                myComm.ExecuteNonQuery();
                myConn.Close();

                MessageBox.Show("delete succeeded");
            }
            catch(Exception x){
                MessageBox.Show(x.ToString());
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            GetAgendaDetails(e.Start.Date);
        }

        private void GetAgendaDetails(DateTime x){
            string myQuery = "select input_agenda from agenda where input_date = '" + x.Date.ToString() + "'";
            SqlConnection myConn = new SqlConnection();
            myConn.ConnectionString = "Data Source=localhost; database=EKS; uid=sa; pwd=123; connection lifetime=20; connection timeout=25; packet size=1024;";

            try {
                myConn.Open();
                SqlDataReader myReader = null;
                SqlCommand myCommand = new SqlCommand(myQuery,myConn);
                myReader = myCommand.ExecuteReader();
                while (myReader.Read()) {
                    textBox1.Text = myReader.GetString(100);
                }
                myConn.Close();
            }
            catch(Exception z){
                MessageBox.Show(z.ToString());
            }
        }
    }
}

ここに画像の説明を入力

4

1 に答える 1

3

DateSelectedユーザーが日付を選択すると発生する MonthCalendar コントロールのイベントを使用します。

   private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
    {
       AganedaInformation info = GetAgendaDetails(e.Start.Date);
    }

Add a private method to query the database based on the passed selected date

Private AganedaInformation GetAgendaDetails(DateTime selectedDate)
{
  //Add logic to query the database with the selected date and return the information
}
于 2013-09-24T07:31:30.940 に答える