<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" />
使用Submit_click,我想在txtstartdate和txtenddate上选择的两个日期之间将数据插入表中。 txtreason应该对表格中的所有日期重复。</ p>
例如,如果在txtstartdate和txtenddate上选择日期07/30/2018和08/04/2018并输入“ hello”作为txtreason的原因,那么我应该获取介于07/30/2018和08/04 /之间的所有日期数据表的ldate列中的2018年,并且您应该在原因列中对每个单独的日期重复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;
}
}
}
答案 0 :(得分:1)
这就是为什么您应该使用模型并利用适当的类型而不是将所有内容都保留为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).
}
}
答案 1 :(得分:0)
使用以下方法更改日期格式。希望对您有帮助
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;
}
}
}