如何使用VBA复选框删除选定的SQL条目

时间:2017-08-18 09:41:24

标签: sql-server vb.net checkbox sql-delete

我正在开发一个Web工具,它将日期输入到SQL表中。 它工作但它需要改进,我不经常使用VB。 [![在此处输入图像说明] [1]] [1]

我想要的是该工具使用Click-Button删除“我的表中的每个SQL条目(GridView),我在其中选中复选框

我如何在VB.Net中编写代码?

编辑:我使用Microsoft Visual Studio与ASP / VB.Net

编辑:设计代码

<div class="style1">
   &nbsp;
     <b> Datum:&nbsp;&nbsp;&nbsp;&nbsp;
    <BDP:BasicDatePicker ID="datum" runat="server" DateFormat="d" />
    </b>&nbsp;&nbsp;
     <b>Bezeichnung:
    <asp:TextBox ID="txtBezeich" runat="server" Height="18px" Width="180px"></asp:TextBox>
    </b>&nbsp;&nbsp;
     <b>Faktor :
    <asp:TextBox ID="txtfaktor" runat="server" Height="18px" Width="180px"></asp:TextBox>
    </b>&nbsp;&nbsp;
    <br />
    <b>
    <asp:Button ID="cmdAdd" runat="server" Height="22px" 
        style="color: #FFFFFF; font-weight: 700; background-color: #006666" Text="Add" 
        Width="62px" />
    &nbsp;&nbsp;<asp:Button ID="cmdDelete" runat="server" Height="22px" 
        style="color: #FFFFFF; font-weight: 700; background-color: #006666" 
        Text="Delete" Width="62px" />

    &nbsp;&nbsp;<asp:Button ID="cmdUpdate" runat="server" Height="22px" 
        style="color: #FFFFFF; font-weight: 700; background-color: #006666" 
        Text="Update" Width="62px" />
    &nbsp;
    </b>
    <b><br /> Definitionen für Faktor:
  <br />  0: Ganzer Feiertag
  <br /> 0,5 : halber Feiertag
  <br /> 0,33 : ein-Drittel Feiertag
  <br /> usw.
    <br /></b>

</div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="False" 
    AllowSorting="True" AutoGenerateColumns="False" BackColor="#F3F3F3" 
    CaptionAlign="Right" DataSourceID="dsFeiertag" Height="133px" PageSize="25" 
    style="text-align: left; font-family: Calibri; font-size: small; margin-bottom: 0px;" 
    Width="477px" >
    <Columns>

    <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chk_hid1" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField="Datum" HeaderText="Datum" SortExpression="Datum" />
        <asp:BoundField DataField="Bezeichnung" HeaderText="Bezeichnung" SortExpression="Bezeichnung" />
        <asp:BoundField DataField="Faktor" HeaderText="Faktor" SortExpression="Faktor" />

    </Columns>
    <HeaderStyle BackColor="#2E0A31" Font-Underline="False" ForeColor="White" />
</asp:GridView>

编辑:功能代码

Partial Public Class FeiertagsTool

Inherits System.Web.UI.Page

Dim adoBIWEB_BIUSERLogin As New ADODB.Recordset
Dim adoDWH As New ADODB.Connection

Dim cred As ReportServerCredentials
Dim Dstr As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'If UserName = "" Or Pwd = "" Then
    '    Server.Transfer("~/Mainpage.aspx", True)
    '    Exit Sub
    'End If
    GetHit(System.Web.HttpContext.Current.Request.Url.AbsolutePath, Request.UserHostAddress)
    adoDWH.ConnectionString = "DSN=Report_DWH_Stage;User ID=BI_WEB_USER;Password=xs4dwh;"
    adoDWH.CursorLocation = ADODB.CursorLocationEnum.adUseServer

End Sub

Protected Sub cmdUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdUpdate.Click
    adoDWH.Open()

    If txtfaktor.Text <> "" Then

        adoDWH.Execute("update External_Inputs.VOXPARK.Feiertag set Faktor = " & Replace(txtfaktor.Text, ",", ".") & " where Datum =  convert(date,'" & datum.Text & "',104)")
    End If
    If txtBezeich.Text <> "" Then
        adoDWH.Execute("update External_Inputs.VOXPARK.Feiertag set Bezeichnung = '" & txtBezeich.Text & "' where Datum =  convert(date,'" & datum.Text & "',104)")
    End If
    GridView1.DataBind()
    adoDWH.Close()
End Sub

Protected Sub cmdDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdDelete.Click
    adoDWH.Open()
    adoDWH.Execute("delete from External_Inputs.VOXPARK.Feiertag where Datum =  convert(date,'" & datum.Text & "',104)")
    GridView1.DataBind()
    adoDWH.Close()
End Sub


Protected Sub cmdAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles cmdAdd.Click
    adoDWH.Open()
    adoDWH.Execute("insert into  External_Inputs.VOXPARK.Feiertag ([Datum],[Bezeichnung],[Faktor]) values( convert(date,'" & datum.Text & "',104) , '" & txtBezeich.Text & "' ," & Replace(txtfaktor.Text, ",", ".") & ")")
    GridView1.DataBind()
    adoDWH.Close()
End Sub

End Class

[1]:https://i.stack.imgur.com/LnnFg.png enter code here

1 个答案:

答案 0 :(得分:1)

很抱歉延迟回到此状态。昨天太热了,所以我去了游泳池。

在delete命令中,您需要遍历GridView的内容以查找已检查的行。获得行后,您可以轻松获取需要删除的假日文本。您需要遍历GridView本身(而不是基础数据,这是更常见的),因为Checkbox未绑定。

要遍历GridView,请使用以下内容:

Protected Sub cmdDelete_Click(sender As Object, e As EventArgs)
    For Each row As GridViewRow In GridView1.Rows
        Dim test As CheckBox
        test = CType(row.Cells(0).Controls(1), CheckBox)
        If test.Checked = True Then
            MsgBox(row.Cells(1).Text + " must be deleted")
        End If
    Next
End Sub

显然,我有一个消息框,你需要构建一个sql命令字符串来删除匹配row.Cells(1).Text的日期。你现在应该有足够的东西来自己修复其余部分。如果您需要进一步的帮助,请随时回复我。