用于日期时间选择器的C#HttpWebRequest

本文关键字:C#HttpWebRequest 选择器 时间 日期 用于 | 更新日期: 2023-09-27 18:29:59

我正在开发一个C#应用程序,该应用程序将使用位于我的网站中的远程MySQL数据库,该数据库托管在带有PHP&MySQL支持。

我尝试使用datetimepicker在datagridview中获取所选的开始日期和结束日期。

在此处输入图像描述

当我单击按钮时,我的表单包含2个日期时间选择器(dateTimePicker1和datetimepicker 2)。它需要在数据库表中从日期和截止日期进行搜索,并将其显示在数据网格视图中。

我在数据库表中连接我的数据网格视图。但我的问题是,当我选择某个日期并单击按钮时,datagridview会变空。

使用php编码填充datagridview:

PHP文件:(data.PHP)

<?php
$servername = "***.**.***.***";    
$username = "root";
$password = "pasword";
$dbname = "databas";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error)
{
die("Connection failed: " . $mysqli->connect_error);
} 
$result = $mysqli->query("SELECT calldate,time_interval,campaign,agent FROM aster");
while($row = mysqli_fetch_array($result)) 
{
    $rows[] = $row;
}
$result->close();
$mysqli->close();
print(json_encode($rows, JSON_NUMERIC_CHECK));
?>

填充数据网格视图的c#编码:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
namespace HTTPDGV
{
    public partial class Form2 : Form
    {
 class User
        {
            [JsonProperty("calldate")]
            public string calldate { get; set; }
            [JsonProperty("time_interval")]
            public string time_interval { get; set; }
            [JsonProperty("campaign")]
            public string campaign { get; set; }
            [JsonProperty("agent")]
            public string agent { get; set; }
        }
 public Form2()
        {
            InitializeComponent();
        }
   private void Form2_Load(object sender, EventArgs e)
        {
 var request = (HttpWebRequest)WebRequest.Create("http://***.**.***.***/data.php");
            request.Method = WebRequestMethods.Http.Get;
            request.Accept = "application/json";
            WebResponse response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader streamreader = new StreamReader(stream);
            String json = streamreader.ReadToEnd();
            List<User> users = JsonConvert.DeserializeObject<List<User>>(json);
            dataGridView1.DataSource = users;    
          }
       }
     }
   }

它运行良好。

但这是我的问题

PHP文件:(time.PHP)

<?php 
$date1 = $_POST['calldate'];
$date2 = $_POST['calldate'];
$servername = "***.**.***.***";
$username = "root";
$password = "password";
$dbname = "database";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if ($mysqli->connect_error)
{
die("Connection failed: " . $mysqli->connect_error);
} 
$result = $mysqli->query("SELECT calldate,time_interval,campaign,agent FROM aster_agent_performance WHERE calldate BETWEEN'$date1'AND'$date2'");
while($row = mysqli_fetch_array($result)) 
{
    $rows[] = $row;
}
$result->close();
$mysqli->close();
print(json_encode($rows, JSON_NUMERIC_CHECK));
?>

datetimepicker的c#编码:

private void btnSearch_Click(object sender, EventArgs e)
        {
            var date1 = dateTimePicker1.Value.Date.ToString("yyyy-MM-dd");
            var date2 = dateTimePicker2.Value.Date.ToString("yyyy-MM-dd");
            HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create("http://***.**.***.***/time.php");
            myRequest.Method = "GET";
            using (WebResponse response = myRequest.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    XmlTextReader reader = new XmlTextReader(stream);
                    var buffer = Encoding.UTF8.GetBytes("calldate=" + date1 + "AND" + date2 + "");
                    stream.Read(buffer, 0, buffer.Length);
                    StreamReader streamreader = new StreamReader(stream);
                    String json = streamreader.ReadToEnd();
                    string date = JsonConvert.SerializeObject(json);
                    dataGridView1.DataSource = date;
                }
            }
}

当我单击搜索按钮时,datagridview变为空。请在我出错的地方检查我的编码。请建议任何在线链接或教程。

我的问题在这里:http://www.voidasks.com/c_httpwebrequest_for_datetimepicker#answers

有人知道这个编码吗?或者有人能告诉我我做错了吗。

感谢

用于日期时间选择器的C#HttpWebRequest

试试这个:

private void btnSearch_Click(object sender, EventArgs e)
{
   var date1 = dateTimePicker1.Value.Date.ToString("yyyy-MM-dd");
   var date2 = dateTimePicker2.Value.Date.ToString("yyyy-MM-dd");
   HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://***.**.***.***/time.php");
   request.Method = "POST";
   request.ContentType = "application/x-www-form-urlencoded";
   byte[] byteArray = Encoding.UTF8.GetBytes("fromdate= " + date1 + " & todate=" + date2);
   request.ContentLength = byteArray.Length;
   Stream stream = request.GetRequestStream();
   stream.Write(byteArray, 0, byteArray.Length);
   HttpWebResponse response = (HttpWebResponse)request.GetResponse();
   StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
   String json = reader.ReadToEnd();
   List<Users> user = JsonConvert.DeserializeObject<List<Users>>(json);
   dataGridView1.DataSource = user;
相关文章:
  • 没有找到相关文章