如何检查MS Acess中是否存在行以进行验证 [英] How to check if Row exists in MS Acess for validation

查看:85
本文介绍了如何检查MS Acess中是否存在行以进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我解决我的问题:

通过使用VB.NET,我想知道ms Access中是否存在一行.如果确实存在,则应将变量设置为pass = true,否则应为false.

我正在做一个学校申请程序,用户需要选择的是他们希望现在开始的年份,该数据库具有之前的10年等,并且每年10月添加新的年份来接受下一年的申请.

但是我遇到的问题是,如果有人选择在10月之前开始,例如2013年,我需要程序检查数据库,以查看2013年(第12行)的行是否退出,然后如果第12行(2013年)不接受,则接受申请存在给出错误消息.

我该如何实现呢?

我只需要知道如何检查行信息即可.
请有人帮忙

急需帮助!!!

谢谢


用于获取数据的代码:

Can anyone kindly help me with my problem:

Through the use of VB.NET i would like to know if a row in ms access exists or not. If it does exist then it should set the variable pass = true else should be false.

Im doing a school application program and what user needs to select is the year they wish to start now the database has the previous 10 years etc and in October every year it adds the new year to accept application for the next year.

but the problem i have is if someone selects to start for e.g 2013 before october i needed the program to check the database to see if the row of 2013 (row 12) exits if it does then accept application if row 12 (2013) does not exist give error message.

How can i achieve this??

I just need to know how to check the row information..
Please can someone kindly help

Help needed urgently!!!

Thanks


code used to get data:

 sql = "SELECT * FROM App"
   
        

             Dim dsapp As New DataSet("App")
                Dim daapp As New OleDb.OleDbDataAdapter

                daapp = New OleDb.OleDbDataAdapter(sql, cnn)
                daapp.Fill(dsapp, "app")

if x = 2010 then
curyear=0
Elseif x=2011 then
curyear=1
else if x=2012 then
curyear =2

        Try
 Dim tblapp As DataTable
                    tblapp = dsapp.Tables("App")

           cnn.open
            textbox1.text = dsapp.tables("app").rows(curyear).item(1)
textbox2.text= dsapp.tables("app").rows(curyear).item(2)
textbox3.text= dsapp.tables("app").rows(curyear).item(3)


        Catch ex As Exception
            MsgBox(ex.ToString)

        End Try

      

   

    End Function

推荐答案

您尝试了什么代码?您似乎已经掌握了基本概念.您需要做的是查询数据库,然后检查是否返回任何行.就这么简单.您的SQL看起来像这样:

SELECT year FROM table WHERE year=2013

您可以使用它来填充表,然后检查表中是否有任何行(dtTable.Count = 0).或者,您可以将其与执行标量一起使用,看看它是否等于DBNull.ValueNothing,这意味着在数据库中找不到它.

或者您可以像这样使用SQL函数COUNT():
SELECT COUNT(*) FROM table WHERE year=2013
并执行标量.然后,您只需要检查它是否为零即可.

*********更新*********

现在,您已经发布了SQL代码,仅当您将sql行更改为以下内容时,以上内容才有效:
What code have you tried? You seem to already have the basic concept down. What you need to do is query the database, then check if any rows returned. It''s that simple. Your SQL would look something like this:

SELECT year FROM table WHERE year=2013

You could use this to fill a table and then check if the table has any rows (dtTable.Count = 0). Or you could use this with an execute scalar and see if it equals DBNull.Value or Nothing which would mean it wasn''t found in the database.

Or you could use the SQL function COUNT() like this:
SELECT COUNT(*) FROM table WHERE year=2013
and execute scalar. Then you''d just have to check if it was zero.

********* UPDATE *********

Now that you''ve posted your SQL code, the above will only work if you change your sql line to something like this:
sql = "SELECT * FROM App WHERE yearColumn=" & x


其中"yearColumn"是您的App表对包含年份的列的名称.

如果您想继续使用旧的sql行,那很好...您只需要搜索相关年份即可,该年份似乎存储在名为x的变量中. (我建议将变量名称更改为更有意义的名称,例如intYear(如果它是整数值)).为此,只需在填满表格后在表格上执行.Select()即可.它看起来像这样:


Where the "yearColumn" is whatever name your App table has for the column that contains the year.

If you want to continue to use your old sql line, that''s fine...you''ll just want to search it for the year in question, which you appear to be storing in the variable named x. (I''d recommend changing that variable name to something more meanningful, such as intYear if it''s an integer value). To do that, you can simply do a .Select() on the table after you have filled it....it would look something like this:

If tblapp.Select("yearColumn=" & x).Count > 0 Then
   'There is at least one record with the year we are looking for
End If


这篇关于如何检查MS Acess中是否存在行以进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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