我有一个错误“没有给出一个或多个必需参数的值。” [英] I have an Error "No value given for one or more required parameters.".

查看:42
本文介绍了我有一个错误“没有给出一个或多个必需参数的值。”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我解决这个错误..

我的代码是



Please Help Me For That Error..
My Code IS

mycon();
         con.Open();

         //cmd = new OleDbCommand("SELECT attandencecount.workidd, worker.name,"+
         //" attandencecount.count_att FROM (attandencecount INNER JOIN worker ON"+
         //" attandencecount.workidd = worker.workidd) WHERE (MONTH(attandencecount.date_time) = @dt)" +
         //" AND (worker.type = @tp)", con);

         cmd = new OleDbCommand("SELECT attandencecount.workidd, worker.name," +
         " attandencecount.count_att FROM (attandencecount INNER JOIN worker ON" +
         " attandencecount.workidd = worker.workidd) WHERE (worker.type = " + Convert.ToByte(1) + ") AND (MONTH(date_time) = " + Convert.ToDateTime(dateEdit2.Text).Month.ToString() + ")", con);


         //cmd.Parameters.AddWithValue("@tp", Convert.ToByte(1));
        //cmd.Parameters.AddWithValue("@dttt", Convert.ToDateTime(dateEdit2.Text).Month.ToString());
         da = new OleDbDataAdapter(cmd);
         ds = new DataSet();
         da.Fill(ds);

         hajaridataGrid.DataSource = ds.Tables[0];
         con.Close();

推荐答案

您发布的代码不会发生错误。但是,查看注释代码时,SELECT语句的参数为@dt,但是当您添加参数时,添加一个名为@dttt的参数。他们需要匹配然后它才能工作。



确保你按照它的注释方式进行,因为如果你现在离开它就是让自己接受SQL注入。你想使用参数。
The error will not happen with the code you have posted. However, looking at the commented code your SELECT statement has a parameter of @dt but when you add your parameters you add one named @dttt. They need to match and then it would work.

Make sure you do it the way you have it commented out because if you leave it the way it is now you are leaving yourself open to SQL injections. You want to use parameters.


你的代码很好。

由于我不知道你的数据库的结构,我无法帮助你。但是,似乎错误在查询中。

这应该是这样的,

(MONTH(date_time)='+转换.ToDateTime(dateEdit2.Text).Month.ToString()+')



重要的是你必须提供单引号表示SQL中的字符串值。



-KR
Your code is fine.
As I don't know the structure of your database, I can't help with that. But yeah, it seems that the error is in query.
This should be like this,
(MONTH(date_time) = '" + Convert.ToDateTime(dateEdit2.Text).Month.ToString() + "')".

It is important that you must provide single quote for the string values in SQL.

-KR


这篇关于我有一个错误“没有给出一个或多个必需参数的值。”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆