Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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
Sql server 检查数据库中是否已存在数据_Sql Server_Vb.net - Fatal编程技术网

Sql server 检查数据库中是否已存在数据

Sql server 检查数据库中是否已存在数据,sql-server,vb.net,Sql Server,Vb.net,如果数据已经存在于数据库中,是否有人可以帮助我通过以红色显示消息来检查数据是否已经存在于数据库中 我想检查数据库中是否已经存在StaffID,如果已经存在,则用户无法使用该StaffID创建新的配置文件 但我不知道怎么做。有人能帮我做一下代码检查数据库中是否已经存在staffID 非常感谢你的帮助 这是我的代码: <table id="tblBasicInfo"> <tr> <td style="width: 50%"> <

如果数据已经存在于数据库中,是否有人可以帮助我通过以红色显示消息来检查数据是否已经存在于数据库中

我想检查数据库中是否已经存在
StaffID
,如果已经存在,则用户无法使用该
StaffID
创建新的配置文件

但我不知道怎么做。有人能帮我做一下代码检查数据库中是否已经存在
staffID

非常感谢你的帮助

这是我的代码:

<table id="tblBasicInfo">
  <tr>
     <td style="width: 50%">
        <div class="form-group">
           <label class="col-sm-3 control-label no-padding-right" for="form-field-1"><span style="color: red">*</span>Staff ID</label>
            <div class="col-sm-8">
                <asp:DropDownList ID="ddlStaffID" class="chosen-select form-control col-sm-9" Width="100%" data-placeholder="Choose StaffID" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlStaffID_SelectedIndexChanged"></asp:DropDownList>
            </div>
         </div>
      </td><tr/>
  <tr>
     <td style="width:50%">
       <div class="form-group">
          <label class="col-sm-3 control-label no-padding-right" for="form-field-1"><span style="color: red">*</span> Full Name</label>
            <div class="col-sm-8">
             <asp:TextBox class="form-control" placeholder="Enter your First Name" id="txtFirstName" runat="server"></asp:TextBox>
            </div>
       </div>
     </td>
     <td style="width: 50%">                                            
      <div class="form-group">
      <label class="col-sm-3 control-label no-padding-right" for="form-field-1"><span style="color: red">*</span> Last Name</label>
        <div class="col-sm-8">
          <asp:TextBox class="form-control" placeholder="Enter your Last Name" id="txtLastName" runat="server"></asp:TextBox>
          <span class="help-inline col-xs-12 col-sm-7" />
        </div>
     </div>
    </td>
 </tr>
 <tr>
   <td style="width:50%">
     <div class="form-group">
       <label class="col-sm-3 control-label no-padding-right" for="form-field-1"><span style="color: red">*</span> Email</label>
        <div class="col-sm-8">
         <asp:TextBox class="form-control" placeholder="Enter your Email Address" id="txtEmail" runat="server"></asp:TextBox>
         <span class="help-inline col-xs-12 col-sm-7" />
        </div>
       </div>
   </td>
   <td style="width:50%">
     <div class="form-group">
     <label class="col-sm-3 control-label no-padding-right" for="form-field-1"><span style="color: red">*</span> Mobile Phone</label>
     <div class="col-sm-8">
      <asp:TextBox class="form-control" placeholder="Enter Mobile Phone Number" id="txtMobilePhone" runat="server"></asp:TextBox>
      <span class="help-inline col-xs-12 col-sm-7" />
      </div>
     </div>
   </td>
 </tr>
SQL查询

IF @Action=101
BEGIN
    SELECT DISTINCT StaffID AS [StaffID], ParticipantID AS [ID], ParticipantName AS [Name]
    FROM mstUser
    WHERE StatusID = '1' AND GroupID = '12'
    ORDER BY StaffID
END

假设StatusID是mstUser的主键 如果你只需要检查记录是否存在,那么

Private Sub OPCode2()
    Dim count As Integer
    Using cn As New SqlConnection("Your connection string")
        Using cmd As New SqlCommand("Select Count(*) From mstUser Where StatusID = @StatusID", cn)
            cmd.Parameters.Add("@StatusID", SqlDbType.Int).Value = ddlStaffID.SelectedItem.Value
            cn.Open()
            count = CInt(cmd.ExecuteScalar())
        End Using
    End Using
    If count > 0 Then 'Record exists
        'You red alert code here.
        'If showing the alert is the problem then that is another question
    End If
End Sub
编辑

根据@marc_使用的代码(如果存在)

Private Sub OPCode3()
    Dim Exists As Boolean
    Using cn As New SqlConnection("Your connection string")
        Using cmd As New SqlCommand("If Exists (Select * From mstUser Where StatusID = @StatusID) Select 1 Else Select 0;", cn)
            cmd.Parameters.Add("@StatusID", SqlDbType.Int).Value = ddlStaffID.SelectedItem.Value
            cn.Open()
            Exists = CBool(cmd.ExecuteScalar())
        End Using
    End Using
    If Exists Then 'Record exists
        'You red alert code here.
        'If showing the alert is the problem then that is another question
    End If
End Sub

“如果数据库中已经存在数据,任何人都可以通过以红色显示消息来帮助我检查数据库中是否已经存在数据。”。这完全是不合理的。您不会通过显示方法来检查数据库。如果要检查数据库中是否存在数据,则需要查询数据库。然后根据该查询的结果显示一条消息。它们是两个完全独立的东西。如果你不知道如何查询数据库,那么你应该从学习开始。如果您知道,那么您已经知道如何检查数据是否存在。编辑代码以反映“记录存在”。更改为>警告词:
在一个大表上选择计数(*)
可能需要很长时间才能完成-最好重写此检查以使用
(如果存在)(
logic@marc_s谢谢你的更正。如果我理解正确,If Exist将在找到匹配项后立即返回,并且计数(*)必须始终扫描整个表。对如果该记录不存在,则计时将是相同的。因此,如果1亿行中的第三行已经与标准匹配,
if EXISTS()
将停止并返回“true”,而
COUNT(*)
将继续计算所有1亿行中的出现次数-即使您对实际计数不感兴趣-问题在于是否至少存在一个事件。。。。
Private Sub OPCode3()
    Dim Exists As Boolean
    Using cn As New SqlConnection("Your connection string")
        Using cmd As New SqlCommand("If Exists (Select * From mstUser Where StatusID = @StatusID) Select 1 Else Select 0;", cn)
            cmd.Parameters.Add("@StatusID", SqlDbType.Int).Value = ddlStaffID.SelectedItem.Value
            cn.Open()
            Exists = CBool(cmd.ExecuteScalar())
        End Using
    End Using
    If Exists Then 'Record exists
        'You red alert code here.
        'If showing the alert is the problem then that is another question
    End If
End Sub