Methods about determining whether the input data is in the database. ]

xiaoxiao2021-03-06  45

Methods about determining whether the input data is in the database. ]

problem:

If you want to enter a user name, how can I determine that the name is existing in the database; or you have entered a password, how to determine it is correct? I have summarized several:

1. Using a Find () method

Read the data to a DATATABLE to create a primary key.

Then define a DATAROW variable so that it is equal to DATATABLE.FIND ("" "The value corresponding to the value to be determined")

If the row exists, the value of the other fields of this line can be more compared to the value to be determined.

Note: This method first is the value of the primary key. Otherwise it is not available. Refer to my "a full code of a login form" that article. This method is applied when the user name is displayed in the drop-down list box, but this method is applied when the user ID is actually stored. Similar places to use this method are also more.

E.g:

DataTable1.primarykey = new datacolumn () {"dataable1.column (" id ")"}

Dim Getrow As DataRow

Getrow = DataTable1.Rows.Find (10) 'Suppose 10 is the primary key for the user input text

IF getrow is nothing then

'Description The corresponding main key value is wrong.

Else

DIM STRNAME AS STRING = GETROW ("Name")

If strname = textbox1.text.trim ("") THEN

'This record exists

Else

'This record does not exist

END IF

END IF

2. Use the command.executeAlar () method.

This method directly executes the SQL statement (generally more aggregated functions, the number of returns is pre-known)

For example: DIM CMD AS New Sqlcommand ("Select Count (*) from table1 where name = 'Zhang 3'", SQLConnection1)

Dim count as integer = cmd.executesealar ()

IF country <1 THEN

'There is no match

Else

'There is a matching line

END IF

3. Using the command.executeReader () method

We know, so that the speed is relatively fast, but the record is less than the records, it is not obvious at speed, and there is nothing advantage.

For example: DIM CMD AS New Sqlcommand ("SELECT * from Table1 Where Name = 'Zhang 3'", SQLConnection1)

Dim Reader as SqldataReder

Reader = cmd.executeReader ()

IF reader.read then

'There are data readable, explaining this record

Else

Conversely, the explanation does not exist

END IF

4. Using the DATAROW.SELECT () method

This method does not need to define primary keys, which can be applied to any field. However, the speed is slower than the first method.

E.g:

DIM filterRow () AS DATAROW

Filterrow = DataTable1.select (Retrieval expression) 'Note: Expressions can be written directly. Such as: "Name = 'Zhang San'"

IF filterrow.length <1 THEN

'This record does not exist

Else

'This record exists

END IF

5. Use DataView

It is different from the DataTable SELECT method, and the latter returns a DataRow array from the table, although its content reflects the changes to the base table, but its member relationships and sort remain static. DataView's dynamic features make it ideal for data binding applications. This method is most convenient if you need to sort the fields.

DIM DVIEW1 AS New DataView (DataTable1, "Name = 'Zhang 3", "Name", DataViewRowState.currentrows)

If DVIEW1.COUNT <1 THEN

'Record does not exist

Else

'Record existence

END IF

转载请注明原文地址:https://www.9cbs.com/read-66561.html

New Post(0)