gpt4 book ai didi

c# - 错误 : No value given for one or more required parameters

转载 作者:太空狗 更新时间:2023-10-29 23:15:27 25 4
gpt4 key购买 nike

我正在尝试通过比较 dataetimepicker 的 month and year value 与存储在 Date/Time 类型的数据库列中的日期来获取 Id,如下是我用来执行此操作的查询,但它在这一行显示以下错误 while (rs.Read())

错误

No value given for one or more required parameters.

代码

public int GetDrID_MonthWise(string DrName,int month,int year, int refDrID)
{
int data = 0;
try
{
string sql = "Select d.DoctorID From Doctor_Master d,Patient_registration p where d.LastName + ' ' + d.FirstName = '" + DrName + "' AND datepart(mm,p.[RegDate])=@month AND datepart(yyyy,p.[RegDate])=@year AND p.DoctorID=" + refDrID;
cmd = new OleDbCommand(sql, acccon);
cmd.Parameters.AddWithValue("@month", month);
cmd.Parameters.AddWithValue("@year", year);
rs = cmd.ExecuteReader();
while (rs.Read())
{
data = Convert.ToInt32(rs[0]);
}
}
catch (Exception err)
{
MessageBox.Show(err.Message.ToString());
}
return data;
}

我像这样传递月份和年份值,我在哪里做错了?

int month = dateTimePickerMonth.Value.Month;
int year = dateTimePickerMonth.Value.Year;

最佳答案

您的查询有误,请按此操作,

 string sql = "Select d.DoctorID From Doctor_Master d,Patient_registration p where d.LastName + ' ' + d.FirstName = '" + DrName + "' AND datepart("m",p.[RegDate])=@month AND datepart("yyyy",p.[RegDate])=@year AND p.DoctorID=" + refDrID;

注意:您必须在 ms Access 的 DatePart 函数中用 ""括起日期间隔,并且只能用单个 m 表示月份。如果您没有用“”括起yyyy和m,系统认为yyyy和m是参数和这些参数的期望值。

If you execute your query in Ms-Aceess sql view then it will show thismessage "Enter Paremeter Value".

enter image description here

关于c# - 错误 : No value given for one or more required parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19463534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com