我正在尝试检查重复记录,其中一个字段是日期... TxtNum
是文本框,DtDate
是DateTimePicker,还有一个名为CHECK_DUPLICATE
的存储过程
在我的情况下,我不想检查确切的日期..但仅限年份..例如..如果两个条目为txtNum = 1000
且两个字段的年份值为{,则记录重复{1}}
2017
SQL:
public DataTable CHECK_DUPLICATE(string txtNum, string dtDateYear)
{
DataTable dt = new DataTable();
SqlParameter[] param = new SqlParameter[2];
param[0] = new SqlParameter("@txtNum", SqlDbType.NVarChar, 50);
param[0].Value = txtNum;
param[1] = new SqlParameter("@dtDateYear ", SqlDbType.NVarChar, 5);
param[1].Value = dtDateYear;
dt = DAL.SelectData("CHECK_PROCURATION_DUPLICATE", param);
DAL.close();
return dt;
}
private void btnAddProcuration_Click(object sender, EventArgs e)
{
string year = DtDate.Value.ToString("yyyy");
DataTable dt = new DataTable();
dt = CHECK_DUPLICATE (txtNum.Text, year);
if (dt.Rows.Count > 1)
MessageBox.Show("Duplicated records", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
但是它不起作用......它保存了记录,即使它是重复的。
答案 0 :(得分:1)
MyDate = @Year
应该DATEPART(YEAR, MyDate) = @Year
喜欢:
CREATE PROCEDURE [dbo].[CHECK_DUPLICATE]
@Num nvarchar (50),
@Year nvarchar (5),
AS
BEGIN
SELECT Num, DATEPART(YEAR, MyDate) AS 'Year'
FROM tblMyList
WHERE Num = @Num AND DATEPART(YEAR, MyDate) = @Year
END