C# - OleDb数据适配器 [英] C#- OleDb Data Adapter

查看:72
本文介绍了C# - OleDb数据适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。



这是我的计划。

Hi.

This is my program.

try
{
con.open();
{
da=new oledbdataadapter("Select modelname from table where type='gate' and min<='"+textbox1.text+"' and max>=textbox1.text+"'",con);
ds.Fill(da,"main");
if(tables[0].rows.count>1)
{
datagridview1.datasource=ds;
datagridview1.datamember=main;
}
else
{
datagridview1.datasource=null;
}





这里我给出了每个型号的最小和最大范围。如果我在textbox1中给出任何数字,它应该比较最小和最大范围之间的值并给出结果..



例如;



型号名称:zeta,

min = 200
max = 250

type = gate



如果我给210,它应该给出相应的型号名称那个范围。



但它显示所有有2,1,0,21,20,210,10的字段。



请帮我解决这个问题。



Here i given min and max range for every model.If i give any number in textbox1 , it should compare the values between min and max range and gives the result..

For example;

Model Name : zeta,
min=200
max=250
type=gate

If i give 210, it should give respective model name which have comes in that range.

but it displays all fields which have 2,1,0,21,20,210,10.

please help me for this issue.

推荐答案

你好,



这种行为可能是因为您要求数据库返回整数值(返回值min /来自数据库的最大值)基于字符串(文本)输入。您可以尝试将文本框值解析为如下整数:



Hi there,

This behaviour could be caused because you're asking the database to return an integer value (the return value min/max from the database) based on a string (text) input. You could try to parse the textbox value to an integer like :

int i = Convert.ToInt32(textbox1.Text.Trim());



然后使用sql查询将该值提供给数据库(请注意引号):


and then feed that value to the database using an sql query (mind the quotes) :

da=new oledbdataadapter("Select modelname from table where type='gate' and min <= i and max >= i",con);





我也注意到原始查询中的拼写错误,不确定这是否是复制/粘贴问题,但如果你这样使用它肯定会返回一个不合适的值。你忘了几个引号:



da = new oledbdataadapter(SELECT modelname FROM table WHERE type ='gate'AND min< = '+ textbox1.text +'AND max> ='+ textbox1.text +',con);



引号的不正确使用导致数据库查询或数据库查询失败的无效结果的90%。希望这有助于



also I notice a typo in your original query, not sure if that's a copy/paste issue but it will certainly return an inproper value if you use it like that. You forgot a couple of quotes :

da = new oledbdataadapter("SELECT modelname FROM table WHERE type='gate' AND min <= '" + textbox1.text + "' AND max >= '" + textbox1.text + "'",con);

The inaccurate use of quotes is responsible for 90% of invalid results of database queries or database query failures. Hope this helps


这篇关于C# - OleDb数据适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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