PDA

View Full Version : سوال: مشکل با متد AddDay در تاریخ شمسی



jafarpalideh
سه شنبه 18 مهر 1396, 08:01 صبح
با سلام
من وقتی میام و داده ها رو وارد دیتابیس میکنم با AddDay تاریخهای 1396/07/31 یا مثلا 1396/08/31 سیو میشه !!!
مشکل کجاست؟




if (radioButton1.Checked == true)
{
string PreventiveId = dataGridView1.CurrentRow.Cells["PreventiveId"].Value.ToString();
int period = Convert.ToInt32(dataGridView1.CurrentRow.Cells["PeriodDay"].Value.ToString());
string startDate = txtDateBegin.Text;
string endDate = txtDateEnd.Text;
CultureInfo farsiCultureInfo = new CultureInfo("fa-ir");
DateTime dtStart = DateTime.Parse(startDate, farsiCultureInfo);
DateTime dtEnd = DateTime.Parse(endDate, farsiCultureInfo);
sconnection = new SqlConnection();
sconnection.ConnectionString = PM.Properties.Settings.Default.ConPM;
if (sconnection.State == ConnectionState.Closed)
{
sconnection.Open();
}
for (DateTime nextdate = dtStart; nextdate <= dtEnd; nextdate = farsiCultureInfo.Calendar.AddDays(nextdate, period))
{
string Insert2 = "Insert into TblPreventiveDay values ('{0}','{1}','{2}','{3}')";
string nnextdate = string.Format("{0:yyyy/MM/dd}", nextdate);
Insert2 = string.Format(Insert2, PreventiveId, nnextdate, 0, 0);
Function.DoDml2(Insert2,sconnection);
}
sconnection.Close();
MessageBox.Show("پیشگیرانه طی دوره روزانه ایجاد شد");


}











public static void DoDml2(string Query,SqlConnection con)
{
SqlCommand cmd = new SqlCommand(Query, con);
cmd.ExecuteNonQuery();


}








146661

jafarpalideh
سه شنبه 18 مهر 1396, 11:15 صبح
مشکل رو حل کردم :
اول تاریخ میلادی رو به شمسی تبدیل کردم (قبل از Insert)


public string MiladiToShamsi(DateTime _date)
{
PersianCalendar pc = new PersianCalendar();
StringBuilder sb = new StringBuilder();
sb.Append(pc.GetYear(_date).ToString("0000"));
sb.Append("/");
sb.Append(pc.GetMonth(_date).ToString("00"));
sb.Append("/");
sb.Append(pc.GetDayOfMonth(_date).ToString("00"));
return sb.ToString();




}



و بعد هم :


string PreventiveId = dataGridView1.CurrentRow.Cells["PreventiveId"].Value.ToString();
int period = Convert.ToInt32(dataGridView1.CurrentRow.Cells["PeriodDay"].Value.ToString());
string startDate =shamsitomiladi( txtDateBegin.Text);
string endDate =shamsitomiladi( txtDateEnd.Text);
DateTime dtStart = DateTime.Parse(startDate);
DateTime dtEnd = DateTime.Parse(endDate);
PersianCalendar pc = new PersianCalendar();
sconnection = new SqlConnection();
sconnection.ConnectionString = PM.Properties.Settings.Default.ConPM;
if (sconnection.State == ConnectionState.Closed)
{
sconnection.Open();
}
for (DateTime nextdate = dtStart; nextdate <= dtEnd; nextdate = pc.AddDays(nextdate, period))
{
string Insert2 = "Insert into TblPreventiveDay values ('{0}','{1}','{2}','{3}')";
string shamsi = MiladiToShamsi(nextdate);
Insert2 = string.Format(Insert2, PreventiveId, shamsi, 0, 0);
Function.DoDml2(Insert2, sconnection);
}
sconnection.Close();
MessageBox.Show("پیشگیرانه طی دوره روزانه ایجاد شد");