1

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

我需要将日期传递给我的 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 所提到的,我如何能够传递我的价值

4

4 回答 4

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;
 }
}

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

于 2013-02-25T05:46:14.637 回答
1

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

于 2013-02-25T05:55:53.960 回答
1

你的数据库表列的数据类型是Date,你可以试试DateTime还是一个datetime2

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

com.Parameters.Add(database.CreateParameter("@Admitdate",PatientRegistration.AdmitDate))
于 2013-02-25T05:12:18.367 回答
0
cmd.Parameters.AddWithValue("@DATE", DateTime.Now);
于 2013-11-26T15:03:47.343 回答