简体   繁体   English

在窗体C#Visual Studio中比较两个日期起始日期和数据库SQL Server中的日期

[英]Compare Two Date From Date at Form C# Visual Studio and Date From Database SQL Server

I want make a auto number based date and number at "Transaction" Form. 我想在“交易”表格上创建一个基于自动编号的日期和数字。 But i have problem with make a "condition" to compare date "today" and date "yesterday". 但是我有一个使“条件”比较日期“今天”和日期“昨天”的问题。 If date is different, then will make a new "autonumber" from number 1. For example, that date is 2019-08-08 so the ID from "permintaanId" is P2019080803 (two last number is how many transaction make that day). 如果日期不同,则将从数字1中创建一个新的“自动编号”。例如,该日期为2019-08-08,因此来自“ permintaanId”的ID为P2019080803(最后两个数字是当天进行的交易次数)。 And tomorrow is 2019-08-09 will make ID P2019080901 (two last number will reset because no one transaction make) 明天是2019-08-09,它将产生ID P2019080901(由于没有人进行交易,因此将重置最后两个数字)

private void auto()
   {
       long hitung;
       string urut;
       SqlConnection conn = konn.GetConn();
       conn.Open();
       cmd = new SqlCommand("select permintaanId from permintaan_data where permintaanId in(select max(permintaanId) from permintaan_data) order by permintaanId DESC", conn);
       rd = cmd.ExecuteReader();
       rd.Read();
       if (rd.HasRows) //<- this condition
       {
           hitung = Convert.ToInt64(rd[0].ToString().Substring(rd["permintaanId"].ToString().Length - 2, 2)) + 1;
           string joinstr = "00" + hitung;
           urut = "P" + DateTime.Now.ToString("yyyyMMdd") + joinstr.Substring(joinstr.Length - 2, 2);
       }
       else
       {
           urut = "P" + DateTime.Now.ToString("yyyyMMdd") + "01";
       }
       rd.Close();
       txt_noPermintaan.Text = urut;
       conn.Close();
   }
    DECLARE @permintaanId INT=0
DECLARE @NewNo VARCHAR(40)=''

select @permintaanId=CAST(ISNULL(MAX(RIGHT(permintaanId,1)),0)+1 AS INT)
FROM permintaan_data
WHERE CAST([tranData] AS DATE) =CAST(GETDATE() AS DATE)

SET @NewNo='P'+CONVERT(VARCHAR(10),GETDATE(),112)+CAST(@permintaanId AS VARCHAR(10))

SELECT @NewNo

get last item created 获取最后一个创建的项目

cmd = new SqlCommand("select top1 permintaanId from permintaan_data order by DESC", conn);
rd = cmd.ExecuteReader();

if (rd.HasRows)
{
    var id = rd["the one with p and 2 numbers at the end"].ToString();
    var dateLastCreated = id.ToString().Remove(0,1).Remove(id.Length-2,2);
   if (dateLastCreated == DateTime.Now.ToString("yyyyMMdd"))
   {
       //increment
   }
   else
   {
       //create new
   }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中的控制台应用程序中将SQL Server 2008数据库中的日期(仅)与系统日期(仅)进行比较 - How to compare date(only) from SQL Server 2008 database to the system date(only) in console application in C# Microsoft SQL Server 数据库/Visual Studio C#:日期处理 - Microsoft SQL Server database / Visual Studio C# : date handling 使用 SqlTransaction 将日历选择器中的日期存储到 SQL Server 中 - Visual Studio 2015/C# - Store date from calendar picker into SQL Server using SqlTransaction - Visual studio 2015/C# 如何比较sql数据库中的日期与当前日期以允许访问C#mvc5中的页面 - How to compare a date from sql database with current date to allow access page in C# mvc5 比较日期 C# 和 SQL 服务器 - Compare Date C# and SQL Server C# SQL Server 比较日期和字符串 - C# SQL Server compare date with string 如何将日期时间选择器中的日期值转换为Visual C#中SQL Server表中的“日期”列? - How to convert the date value taken from the date time picker to the Date column in SQL Server table in Visual C#? 从C#中的数据库比较2个不同的日期/时间 - Compare 2 different date/time from database in C# 如何使用C#比较数据库中的日期? - How to compare date from database using C#? 比较SQL中的日期c# - compare date in SQL c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM