在c#中将日期插入数据库

时间:2013-02-25 05:06:16

标签: c# .net sql-server-2008

我发现很多关于此问题的问题,我尝试了一切,但还没有成功。

我需要将日期传递给我的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;
     }
}

我尝试了什么:

如上所述Link

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

this link

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

然后我尝试了this link

 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所提及的那样传递我的价值

4 个答案:

答案 0 :(得分:2)

我仅修改了@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;
 }
}

如果您需要更多帮助,请与我们联系。

答案 1 :(得分:1)

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

并且不要将Date变量括在引号中:

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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