Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 从数据表中查找条目_Vb.net_Sql Server 2008 - Fatal编程技术网

Vb.net 从数据表中查找条目

Vb.net 从数据表中查找条目,vb.net,sql-server-2008,Vb.net,Sql Server 2008,我有一个WinForm,里面有文本框。 当我激活TextBox.Leave事件时,我需要检查该条目TextBox.text在我的数据集中(列,而不是行)该条目是否存在,如果不存在,我只需获得一个MsgBox(“您输入的文本已经存在,请使用另一个”)并且不允许将其提交到数据库中。您可以将sql查询编写为select cols from tablename,其中col='yourtextboxvalue' 使用commandObject.executeReader执行datareader,并在try

我有一个WinForm,里面有文本框。

当我激活
TextBox.Leave
事件时,我需要检查该条目
TextBox.text
在我的
数据集中(列,而不是行)该条目是否存在,如果不存在,我只需获得一个
MsgBox(“您输入的文本已经存在,请使用另一个”)
并且不允许将其提交到数据库中。

您可以将sql查询编写为select cols from tablename,其中col='yourtextboxvalue' 使用commandObject.executeReader执行datareader,并在try块中执行datareader.Read()方法。如果值不存在,它将转到catch块并在那里显示messagebox。 类似于检查用户名

SqlCommand command = new SqlCommand("Select Password from tblUser where UserName=@username", connection);
            command.Parameters.AddWithValue("@username", txtUsername.Text);     //add parameter to the query

 if (dataReader.Read())      //if any record is available
            {
                dbPassword = dataReader["Password"].ToString();     //get the password for entered username
                if (dbPassword != "" && dbPassword.Equals(password))        
                {

                }

                else
                    MessageBox.Show("Invalid Password","Invalid input");
                connection.Close();     //close the connection
            }

            else 
                MessageBox.Show("Username not found", "Invalid input");     //message will be shown if no record found for the entered user ie.username doesn't exist

从数据库的角度来看,防止列中出现重复条目的最简单方法是在给定列上设置唯一约束:

CREATE TABLE table1
(
Id int NOT NULL,
Name varchar(255) NOT NULL,
CONSTRAINT uc_Name UNIQUE (Name)
) 

将此代码放入textbox1 leave事件中

Dim result() As DataRow = DataTable.Select("ColumnName = '" + TextBox1.Text.Trim() + "'")

如果result.length>0,则不要保存它….

因为您提到了数据集,请尝试此操作(数据集不是表,而是表的集合)


试试这个代码,我为你复制它,它工作正常。。。仔细检查您在文本框中输入的值

Public Class Form1
Dim table As DataTable
Dim ds As DataSet
Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave

    Dim result() As DataRow = ds.Tables(0).Select("Name = '" + TextBox1.Text.Trim() + "'")
    If (result.Length > 0) Then
        MsgBox("Value Exist")
    Else
        'do your calculation
    End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    table = New DataTable("Players")

    ' Add two columns.
    table.Columns.Add(New DataColumn("Name", GetType(String)))
    table.Columns.Add(New DataColumn("age", GetType(String)))
    ds.Tables(0).Select()

    table.Rows.Add("Magesh", "25")
    table.Rows.Add("flook", "22")
    ds.Tables.Add(table);


End Sub
End Class

您可以尝试将列中的值放入一个列表,然后在IF语句中使用list.Contains。@JonathonColley这将是一个非常缓慢的过程,因为该表有超过250.000个entries。我正在尝试这样做,而不去服务器,并且为了防止使用服务器,因为速度太快,因为在我的例子中,我已经用所有信息填充了数据集。我正在尝试在不去服务器的情况下这样做,并且为了防止使用服务器,因为在我的例子中,我已经用所有信息填充了数据集。我不应该说我的数据集的名称吗?你能从数据集中检索你的数据表吗?然后检查数据表。选择(yourcolumnname=textbox.text)我想我必须首先指定我的
DataSet
在这种情况下?像这个DataSet.Tables(0)一样使用。选择器
DataSet.Tables(“tablename”)
我已经尝试过这个方法:Dim Tot()作为DataRow=Me.DataSet.DataTab;e、 如果Tot.Length.ToString>0,则选择(“Column='”+TextBox3.Text.Trim()+”),然后选择MsgBox(“Clan sa ovim brojem clanske karte vec postoji!”)Me.TextBox4.Focus()Else退出Sub-End If,它只保留text.box,因为一切正常。请参阅更新的数据集类中的.Tables。尝试在textbox leave事件中发布代码,指定在textbox中输入的值和数据集值
Dim Tot() as DataRow = yourDataset.Tables(IndexNumberOfTable).Select("theColName= '" + TextBox.Text.Trim() + "'")
Public Class Form1
Dim table As DataTable
Dim ds As DataSet
Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave

    Dim result() As DataRow = ds.Tables(0).Select("Name = '" + TextBox1.Text.Trim() + "'")
    If (result.Length > 0) Then
        MsgBox("Value Exist")
    Else
        'do your calculation
    End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    table = New DataTable("Players")

    ' Add two columns.
    table.Columns.Add(New DataColumn("Name", GetType(String)))
    table.Columns.Add(New DataColumn("age", GetType(String)))
    ds.Tables(0).Select()

    table.Rows.Add("Magesh", "25")
    table.Rows.Add("flook", "22")
    ds.Tables.Add(table);


End Sub
End Class