如何使用 VBA 复选框删除选定的 SQL 条目
How to delete selected SQL Entries with a VBA Checkbox
我正在开发一个网络工具,它可以将日期输入 SQL table。
它有效,但需要改进,我不经常使用 VB。
[![在此处输入图片描述][1]][1]
我想要的是该工具从我的 table 中删除每个 SQL 条目 (GridView),并使用“单击按钮”,我在其中勾选了复选框
如何在 VB.Net 中编码?
编辑:我使用 Microsoft Visual Studio 和 ASP/VB.Net
编辑:设计代码
<div class="style1">
<b> Datum:
<BDP:BasicDatePicker ID="datum" runat="server" DateFormat="d" />
</b>
<b>Bezeichnung:
<asp:TextBox ID="txtBezeich" runat="server" Height="18px" Width="180px"></asp:TextBox>
</b>
<b>Faktor :
<asp:TextBox ID="txtfaktor" runat="server" Height="18px" Width="180px"></asp:TextBox>
</b>
<br />
<b>
<asp:Button ID="cmdAdd" runat="server" Height="22px"
style="color: #FFFFFF; font-weight: 700; background-color: #006666" Text="Add"
Width="62px" />
<asp:Button ID="cmdDelete" runat="server" Height="22px"
style="color: #FFFFFF; font-weight: 700; background-color: #006666"
Text="Delete" Width="62px" />
<asp:Button ID="cmdUpdate" runat="server" Height="22px"
style="color: #FFFFFF; font-weight: 700; background-color: #006666"
Text="Update" Width="62px" />
</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.pngenter code here
很抱歉延迟回复此问题。昨天太热了,就去游泳池了
在您的删除命令中,您需要遍历 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 的日期。你现在应该有足够的钱自己修理剩下的了。如果您需要进一步的帮助,请随时与我联系。
我正在开发一个网络工具,它可以将日期输入 SQL table。 它有效,但需要改进,我不经常使用 VB。 [![在此处输入图片描述][1]][1]
我想要的是该工具从我的 table 中删除每个 SQL 条目 (GridView),并使用“单击按钮”,我在其中勾选了复选框
如何在 VB.Net 中编码?
编辑:我使用 Microsoft Visual Studio 和 ASP/VB.Net
编辑:设计代码
<div class="style1">
<b> Datum:
<BDP:BasicDatePicker ID="datum" runat="server" DateFormat="d" />
</b>
<b>Bezeichnung:
<asp:TextBox ID="txtBezeich" runat="server" Height="18px" Width="180px"></asp:TextBox>
</b>
<b>Faktor :
<asp:TextBox ID="txtfaktor" runat="server" Height="18px" Width="180px"></asp:TextBox>
</b>
<br />
<b>
<asp:Button ID="cmdAdd" runat="server" Height="22px"
style="color: #FFFFFF; font-weight: 700; background-color: #006666" Text="Add"
Width="62px" />
<asp:Button ID="cmdDelete" runat="server" Height="22px"
style="color: #FFFFFF; font-weight: 700; background-color: #006666"
Text="Delete" Width="62px" />
<asp:Button ID="cmdUpdate" runat="server" Height="22px"
style="color: #FFFFFF; font-weight: 700; background-color: #006666"
Text="Update" Width="62px" />
</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.pngenter code here
很抱歉延迟回复此问题。昨天太热了,就去游泳池了
在您的删除命令中,您需要遍历 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 的日期。你现在应该有足够的钱自己修理剩下的了。如果您需要进一步的帮助,请随时与我联系。