【问题标题】:date is not compare to database where combobo selected item日期不与组合框选定项目的数据库进行比较
【发布时间】:2020-01-10 10:51:27
【问题描述】:

我正在创建一个项目,用户必须在 Combobox 中选择日期,但我在 Combobox 中选择了显示错误日期的日期

字符串未被识别为有效的日期时间。' 我正在尝试从 slq 中的数据库中获取日期。 下面是我的代码

            System.Data.DataTable dt;
       SqlConnection con=new SqlConnection(@"Data Source=krupal;Initial Catalog=LOANNOTICE;Integrated Security=True");
        con.Open();
        this.cmbmonth.FormatString = "yyyy/ MM/dd";
        // DateTime dat = Convert.ToDateTime(cmbmonth.SelectedItem.ToString("yyyy/MM/dd"));
        //var date = cmbmonth.Text;
       //var d1 = Convert.ToDateTime(cmbmonth.Text);
       // MessageBox.Show("" + d1);
        string Querie=("SELECT HCODE ,(select top 1  rtrim(Name) From RNSB_TEST.dbo.FC014076 p with(nolock) where  convert(varchar,PrdCd) = convert(varchar,HCODE)) as PrdName,count(*) as NoOfAcc,round(Sum(BAL2802),2) as PrintAmt ,round(SUM(IntAmt),2) as TotalInt,0 as TDSAmount, round(SUM(IntAmt),2) as NetIntAmt, round((Sum(BAL2802) + SUM(IntAmt)),2)as TotalAmt from LOANNOTICE.dbo.UNCLAIM_DEPO with(nolock) where EffeDate='"+Convert.ToDateTime(cmbmonth.SelectedText).ToString("yyyy/MM/dd") + "' group by HCODE union SELECT HCODE, (select top 1  rtrim(Name) From RNSB_TEST.dbo.FC014076 p with(nolock) where  convert(varchar, PrdCd) = convert(varchar, HCODE)) as PrdName,count(*) as NoOfAcc,round(Sum(MatAmt), 2) as PrintAmt ,round(SUM(IntAmt), 2) as TotalInt,round(sum(TDS_Amount), 2) as TDSAmount, round(SUM(Net_Int), 2) as NetIntAmt, round((Sum(MatAmt) + SUM(Net_Int)), 2) as TotalAmt from LOANNOTICE.dbo.UNCLAIM_DEPO_TDR with(nolock) where EffeDate = '"+Convert.ToDateTime(cmbmonth.SelectedText).ToString("yyyy/MM/dd") + "' group by HCODE");
        SqlCommand cmd = new SqlCommand(Querie, con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        dt = new System.Data.DataTable();
        sda.Fill(dt);

任何人有解决方案,请快速告诉我。 谢谢。

【问题讨论】:

    标签: c# sql-server winforms


    【解决方案1】:

    您需要将字符串解析为有效的日期时间。

    您的转换错误,请将字符串转换为日期时间,然后传递给适当的日期字段。

    请参考link

    【讨论】:

      【解决方案2】:

      我注意到你这里有这个空白:

      this.cmbmonth.FormatString = "yyyy/ MM/dd"
      

      无论如何,您应该始终检查以字符串形式传递的日期,如果无法转换,则抛出异常:

      if (!DateTime.TryParse(_date, out DateTime date))
      {
          throw new InvalidCastException("Not a valid date.");
      }
      

      最后,尝试以这种方式设置 SqlCommand 参数,更具可读性,并且您不必处理字符串插值(结帐:

      using (DbConnection connection = new SqlConnection("MyConnectionString"))
      {
          using (DbCommand command = new SqlCommand(connection))
          {
              command.CommandType = System.Data.CommandType.Text;
              command.CommandText = "SELECT * FROM MyTable WHERE Date = @Date";
              command.Parameters.Add("@Date", SqlDbType.Date);
              command.Parameters["@Date"].Value = date;
          }
      }
      

      试一试! :)

      问候!

      【讨论】:

        猜你喜欢
        • 2019-02-18
        • 2015-11-02
        • 1970-01-01
        • 2016-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-18
        • 1970-01-01
        相关资源
        最近更新 更多