根据datagridview自动检查标签值 [英] Automatically check label value against datagridview

查看:86
本文介绍了根据datagridview自动检查标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让系统检查标签中的数字,与数据库中的数字相关(与登录页面类似的概念)然而,用户不需要输入任何信息,但我一直在收到错误...



I am trying to make the system check that the number in the label, correlates with one in the database ( similar concept to a login page) However the user shouldnt need to enter any information but I keep on getting errors...

OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Customer Orders] WHERE [Order ID] = @[Order ID]", MAcon);
da.SelectCommand.Parameters.AddWithValue("@[Order ID]", PrdtID.Text);
DataTable dtbl = new DataTable();
da.Fill(dtbl);





我的尝试:





What I have tried:

OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Customer Orders] WHERE [Order ID] = @[Order ID]", MAcon);
da.SelectCommand.Parameters.AddWithValue("@[Order ID]", PrdtID.Text);
DataTable dtbl = new DataTable();
da.Fill(dtbl);

推荐答案

使用调试器 - 检查语句,是否有意义,是否可以通过另一个查询工具执行DB?

可能有问题;

第一期 - 为什么要命名你的参数@ [订单ID]?

参数名称不应该包含空格,也不应该使用[]括号。查询包含空格或保留字的列时使用[]括号。

将参数名称更改为; @OrderId

其次,您可能会发现AddWithValue将您的值解释为不正确的数据类型,因此您的语句最终为:

Use your debugger - check the statement, does it make sense, can you execute it via another query tool against the DB?
Likely issues are;
First issue - why are you naming your parameter "@[Order ID]"?
Parameter names should not contain spaces, nor should they use [] brackets. the [] brackets are used when querying columns that contain spaces or reserved words.
Change your parameter name to; @OrderId
Secondly, you may find that AddWithValue is interpreting your value as an incorrect data type, hence your statement ends up as;
SELECT * FROM [Customer Orders] WHERE [Order ID] = '2'



当你真正想要


when you actually want

SELECT * FROM [Customer Orders] WHERE [Order ID] = 2



根据您使用的数据库引擎,这可能会也可能不会自动转换 - MS SQL通常会正确返回,但MS Access不会。

将字符串值转换为正确的数据类型 - 很可能是整数,或者使用;


Depending on the database engine you are using this may or may not be automatically converted - MS SQL will normally return correctly but MS Access does not.
Either convert the string value to the correct data type - most likely an integer, or use;

da.SelectCommand.Add("@OrderId", OleDbType.Integer).Value = 2



这将确保您的语句被正确创建。



亲切的问候


This will ensure your statement gets correctly created.

Kind Regards


这篇关于根据datagridview自动检查标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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