在c#中插入数据库日期

本文关键字:数据库 日期 插入 | 更新日期: 2023-09-27 18:01:09

我发现了很多关于这件事的问题,我尝试了一切,但都没有成功。

我需要将日期传递到我的SQL Server数据库。我的代码如下:

public bool CreatePatient()
{
    IDbConnection connection = database.CreateOpenConnection();
    IDbTransaction transaction = database.BeginTransaction(connection);
    try
    {
        GenerateRegNo();
        SQL_STATEMENT = "INSERT INTO TblPatientRegistration(RegistrationNo, HospitalID, NIC, AdmitDate, BHTNo, WardNo) 
                         VALUES(@RegNo, @HospiatlNo, @NIC, @Admitdate, @BHTNo, @WardNo)";
        IDbCommand com = database.CreateCommandTransaction(SQL_STATEMENT, connection, transaction);
        com.Parameters.Add(database.CreateParameter("@RegNo", PatientRegistration.RegistrationNo));
        com.Parameters.Add(database.CreateParameter("@HospiatlNo", PatientRegistration.HospitalID));
        com.Parameters.Add(database.CreateParameter("@NIC", PatientRegistration.NIC));
        com.Parameters.Add(database.CreateParameter("@Admitdate", PatientRegistration.AdmitDate));
        com.Parameters.Add(database.CreateParameter("@BHTNo", PatientRegistration.BHTNo));
        com.Parameters.Add(database.CreateParameter("@WardNo", PatientRegistration.WardNo));
        if (com.ExecuteNonQuery() > 0)
        {
            string updStatement = "Update TblControl set RegNo=RegNo+1";
            IDbCommand com2 = database.CreateCommandTransaction(updStatement, connection, transaction);
            com2.ExecuteNonQuery();
            transaction.Commit();
        return true;
        }
        else
        {
            transaction.Rollback();
            return false;
        }
     }
     catch (Exception)
     {
        transaction.Rollback();
        return false;
     }
}

我尝试过的:

如上所述,链接

  com.Parameters.Add(database.CreateParameter("@Admitdate", PatientRegistration.AdmitDate.ToString("dd/MM/yyyy")));

和这个链接

com.Parameters.Add(database.CreateParameter("@Admitdate", "'"+PatientRegistration.AdmitDate+"'"));

然后我尝试了这个链接

 String UrDate = PatientRegistration.AdmitDate.ToString("dd/MM/yyyy");
 System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
 dateInfo.ShortDatePattern = "dd/MM/yyyy";
 DateTime validDate = Convert.ToDateTime(UrDate, dateInfo);
 com.Parameters.Add(database.CreateParameter("@Admitdate", validDate));

这些方法抛出这个异常:

操作数类型冲突:int与日期不兼容

我从PatientRegistration.AdmitDate接收的值为:{2/25/2013 12:00:00 AM}

我尝试了所有这些方法,但都没能保存。请帮我找出这里的问题。

谢谢。。。

编辑:

我的数据库架构:

CREATE TABLE [dbo].[TblPatientRegistration](
    [RegistrationNo] [char](10) NOT NULL,
    [HospitalID] [char](10) NOT NULL,
    [NIC] [char](10) NOT NULL,
    [AdmitDate] [date] NOT NULL,
    [BHTNo] [varchar](10) NOT NULL,
    [WardNo] [varchar](10) NOT NULL,
    [ReleaseDate] [date] NOT NULL,
 CONSTRAINT [PK_TblPatientRegistration_1] PRIMARY KEY CLUSTERED 
(
    [RegistrationNo] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

这个问题可能在这里已经有了答案(势均力敌(:-->我尝试了那里建议的答案(见选项2(,但它似乎不适用于我的问题

答案:经过几次调查,我发现这个问题是由于我在数据库中的错误造成的。

我还没有将值传递给ReleaseDate字段,它在数据库中设置为default value。默认值是表示与日期不兼容的值。这就是造成这个问题的原因。我如何能够传递Ashok kumar 提到的值

在c#中插入数据库日期

我只在@Admitdate行修改了您的代码。

public bool CreatePatient()
{
    IDbConnection connection = database.CreateOpenConnection();
    IDbTransaction transaction = database.BeginTransaction(connection);
    try
    {
    GenerateRegNo();
    SQL_STATEMENT = "INSERT INTO TblPatientRegistration(RegistrationNo, HospitalID, NIC, AdmitDate, BHTNo, WardNo) 
                     VALUES(@RegNo, @HospiatlNo, @NIC, @Admitdate, @BHTNo, @WardNo)";
    IDbCommand com = database.CreateCommandTransaction(SQL_STATEMENT, connection, transaction);
    com.Parameters.Add(database.CreateParameter("@RegNo", PatientRegistration.RegistrationNo));
    com.Parameters.Add(database.CreateParameter("@HospiatlNo", PatientRegistration.HospitalID));
    com.Parameters.Add(database.CreateParameter("@NIC", PatientRegistration.NIC));
        String admitDate = txtAdmitDate.Text;
        DateTime parsedAdmitDate;
        if (DateTime.TryParseExact(admitDate, "d/M/y", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedAdmitDate))
            PatientRegistration.AdmitDate = parsedAdmitDate;
    //com.Parameters.Add(database.CreateParameter("@Admitdate", PatientRegistration.AdmitDate));
    com.Parameters.Add(database.CreateParameter("@BHTNo", PatientRegistration.BHTNo));
    com.Parameters.Add(database.CreateParameter("@WardNo", PatientRegistration.WardNo));
    if (com.ExecuteNonQuery() > 0)
    {
        string updStatement = "Update TblControl set RegNo=RegNo+1";
        IDbCommand com2 = database.CreateCommandTransaction(updStatement, connection, transaction);
        com2.ExecuteNonQuery();
        transaction.Commit();
    return true;
    }
    else
    {
        transaction.Rollback();
        return false;
    }
 }
 catch (Exception)
 {
    transaction.Rollback();
    return false;
 }
}

如果你需要更多的帮助,请告诉我。

数据库表列的数据类型为Date,您可以尝试DateTime还是datetime2

不要用引号括起Date变量:

com.Parameters.Add(database.CreateParameter("@Admitdate",PatientRegistration.AdmitDate))

command.Parameters.AddWithValue("@yourDateTimeVariable", dateTimeObject);????

cmd.Parameters.AddWithValue("@DATE", DateTime.Now);