使用C#和Ms Access的Oledb数据适配器 [英] Oledb data Adapter using C# and Ms Access
问题描述
嗨。
这是我的程序。
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.
推荐答案
一般提示/提示:
1)在MS Access中min
和max
是函数的名称。强烈建议不要将它用作字段名称,因为有些单词(例如type
,table
)是保留字。
SQL保留字(Access Developer)参考 [ ^ ]
访问中的问题名称和保留字 [ ^ ]
2)不要在代码中连接查询字符串。使用参数!
PARAMETERS Declaration( Microsoft Access SQL) [ ^ ]
关于使用代码中的参数(ADO和ADODB)的所有内容[ ^ ]
最后,您的查询应如下所示:
General tips/hints:
1) In MS Accessmin
andmax
is the name of function. It is strongly recommended to not use it as a name of fields, because some of words (for exampletype
,table
) are reserved words.
SQL Reserved Words (Access Developer Reference[^]
Problem names and reserved words in Access[^]
2) Do not concatenate query string in code. Use parameters!
PARAMETERS Declaration (Microsoft Access SQL)[^]
Everything About Using Parameters from Code (ADO & ADODB)[^]
Finally, your query should looks like:
Parameters [myVal] Integer
SELECT modelname
FROM [table]
WHERE [type]='gate' and [min]<=[myVal] and [max]>=[myVal]
但是,正如我上面提到的,我强烈建议你更改字段的名称!
如何调用以上查询?看看 OleDbCommand.Parameters [ ^ ] property。
But, as i mentioned above, i would strongly recommend you to change the name of fields!
How to call above query? Have a look at OleDbCommand.Parameters[^] property.
试试这种方式。它应该工作假设min和max的数据类型是整数
da = new oledbdataadapter(从表中选择modelname,其中type ='gate'和+ textbox1。 text +min and max,con);
Try this way. it should work assuming that data types for min & max are integer
da=new oledbdataadapter("Select modelname from table where type='gate' and " + textbox1.text +" between min and max",con);
这篇关于使用C#和Ms Access的Oledb数据适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!