Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改文本框';通过单击“日历月”来更新内容_C#_Sql Server_Winforms_Sql Server 2008 - Fatal编程技术网

C# 更改文本框';通过单击“日历月”来更新内容

C# 更改文本框';通过单击“日历月”来更新内容,c#,sql-server,winforms,sql-server-2008,C#,Sql Server,Winforms,Sql Server 2008,我正在尝试为我的winform项目制作一个议程工具。当用户在monthCalendar控件上选择日期时,我想在文本框中显示特定日期的数据库记录。下面您可以看到我的db表设计、我的winform设计以及我收到的代码和异常消息。我怎样才能解决这个问题 *ps:无需建议使用参数化查询。我可以,我最终会改变它 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; us

我正在尝试为我的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());
            }
        }
    }
}


使用MonthCalendar控件的
DateSelected
事件,当用户选择日期时将触发该事件

   private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
    {
       AganedaInformation info = GetAgendaDetails(e.Start.Date);
    }
添加一个私有方法,根据已传递的选定日期查询数据库

Private AganedaInformation GetAgendaDetails(DateTime selectedDate)
{
  //Add logic to query the database with the selected date and return the information
}

VisualStudio的intellisense说:找不到类型或命名空间名称“AganedaInformation”(是否缺少using指令或程序集引用?@TimurAykutYıldırım:)。这只是我添加的一个类名,它可能会将Aganeda信息返回给调用者。如果需要,可以将其更改为void,并在相同的方法中设置textfield值。我根据您的建议更新了代码。当每天的日程内容字符量不同时,如何为GetString()方法指定参数?我还添加了异常的屏幕快照使用myReader[“ColumnName”]从每个列中获取值,然后将其设置为相应的文本框。