使用c在两个日期之间插入mysql数据#

0qx6xfy6  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(296)
<asp:TextBox ID="txtstartdate" class="form-control" autocomplete="off" CssClass="datepicker1" runat="server"></asp:TextBox>

<asp:TextBox ID="txtenddate" class="form-control" autocomplete="off" CssClass="datepicker2" runat="server"></asp:TextBox>

<asp:TextBox id="txtreason" TextMode="multiline"  runat="server" />

<asp:Button ID="submit" CssClass="login" runat="server" Text="Submit" OnClick="Submit_click" />

使用提交按钮,单击我想在txtstartdate和txtenddate上选择的两个日期之间插入数据到表中。txtreason应该对表上的所有日期重复。
例如,如果在txtstartdate和txtenddate上选择日期07/30/2018和08/04/2018,并输入“hello”作为txtreason的原因,则我应该在数据表的ldate列中获取07/30/2018和08/04/2018之间的所有日期,并且每个日期的hello应该在reason列中重复6次。如果您更改日期格式,下面的方法非常有用。

protected void Submit_click(object sender, EventArgs e)
{
        DateTime startdate = Convert.ToDateTime(txtstartdate.Text);
        DateTime enddate = Convert.ToDateTime(txtenddate.Text);
        for (DateTime date = startdate; date <= enddate; date = date.AddDays(1))
        {
            try
            {
                string MyConString = "SERVER=localhost;DATABASE=mydb;UID=myid;PASSWORD=abc123;";
                MySqlConnection connection = new MySqlConnection(MyConString);
                string cmdText = "INSERT INTO approved(agentlogin ,leavetype ,date ,time, reason)VALUES ( @login, @type, @date, 'Full day', @reason)";
                MySqlCommand cmd = new MySqlCommand(cmdText, connection);
                cmd.Parameters.AddWithValue("@login", Label1.Text);
                cmd.Parameters.AddWithValue("@type", ddlleavetype.Text);
                cmd.Parameters.AddWithValue("@date", date);
                cmd.Parameters.AddWithValue("@reason", txtreason.Text);

                connection.Open();

                int result = cmd.ExecuteNonQuery();
                connection.Close();

                //lblError.Text = "Data Saved";

            }
            catch (Exception)
            {
                Console.Write("not entered");
                //lblError.Text = ex.Message;
            }
        }
    }
jm81lzqq

jm81lzqq1#

使用以下方法更改日期格式。希望这有帮助

protected void Submit_click(object sender, EventArgs e)
{
    DateTime startdate = Convert.ToDateTime(txtstartdate.Text);
    DateTime enddate = Convert.ToDateTime(txtenddate.Text);
    for (DateTime date = startdate; date <= enddate; date = date.AddDays(1))
    {
        try
        {
            var shtdate = date.ToShortDateString();
            string MyConString = "SERVER=localhost;DATABASE=mydb;UID=myid;PASSWORD=abc123;";
            MySqlConnection connection = new MySqlConnection(MyConString);
            string cmdText = "INSERT INTO approved(agentlogin ,leavetype ,date ,time, reason)VALUES ( @login, @type, @date, 'Full day', @reason)";
            MySqlCommand cmd = new MySqlCommand(cmdText, connection);
            cmd.Parameters.AddWithValue("@login", Label1.Text);
            cmd.Parameters.AddWithValue("@type", ddlleavetype.Text);
            cmd.Parameters.AddWithValue("@date", shtdate);
            cmd.Parameters.AddWithValue("@reason", txtreason.Text);

            connection.Open();

            int result = cmd.ExecuteNonQuery();
            connection.Close();

            //lblError.Text = "Data Saved";

        }
        catch (Exception)
        {
            Console.Write("not entered");
            //lblError.Text = ex.Message;
        }
    }
}
jckbn6z7

jckbn6z72#

这就是为什么你应该使用模型,并利用适当的类型,而不是保持一切一个简单的 string .
虽然不是十全十美,但朝着正确方向迈出的一步应该是这样的:(请记住,我并没有解决每一个问题。还有很大的改进空间)

public class DateRangeModel
{
    public DateTime From {get; set;}
    public DateTime To   {get; set;}

    public IEnumerable<DateTime> DaysInRange {
        get{
            for(DateTime date = StartDate; date.Date <= EndDate.Date; date = date.AddDays(1))
            {
                yield date;
            }
        }
    }

    public DateRangeModel(string from, string to)
    {
        From = GetDate(from);
        To = GetDate(to);
    }

    private static DateTime GetDate(string string_date)
    {
        DateTime dateValue;
        if (DateTime.TryParse(string_date, out dateValue))
            return dateValue;
        else
            throw new Exception("Unable to convert '{0}' to a date.", string_date);

    }
}

protected void Submit_click(object sender, EventArgs e)
{

    DateRangeModel dateRange = new DateRangeModel(txtstartdate.Text, txtenddate.Text);

    OleDbConnection scn = new OleDbConnection();
    scn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data   Source=/mysource;";

    foreach(var dt in dateRange.DaysInRange)
    {
        OleDbCommand scmd = new OleDbCommand("Insert query", scn);
        scmd.CommandType = CommandType.Text;
        scmd.Parameters.Clear();
        scmd.Parameters.AddWithValue("@date1", dt);
        scmd.Parameters.AddWithValue("reason", txtreason);
        //etc (finish your insert stmt here).
    }  
}

相关问题