0

关于我的最新项目,我还有一些问题。我觉得在过去的几天里我取得了一些相当不错的进步,但我仍然在 SQL 库的一些核心概念上苦苦挣扎,即从特定列读取和删除整行。

在上周,我能够构建一个 web 表单,将 excel 文件保存到服务器,打开这些文件并将数据导出到特定的 SQL 表中,并根据用户通过下拉列表选择的内容将数据绑定到特定的数据网格。

我想要完成的是:另一个下拉列表的动态填充取决于用户从第一个下拉列表中选择的内容。更具体地说,我有 4 个表,在每个表的第一列中我有序列号,如果用户在第一个下拉列表中选择 Table2,我希望第二个下拉列表显示 Table2 的 column1 中的所有序列号。然后,如果用户从第二个淹没中选择一个特定的序列号,它会使用该相关行的第 1-5 列填充一个数据网格。

第二部分是创建一个删除按钮,用户可以在数据网格中显示信息后按下该按钮,从该表中删除序列号条目的整行。

这就是我从其他示例中设法将弗兰肯斯坦放在一起的内容:

 Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As System.EventArgs)
    DropDownList2.Enabled = True 'its remains disabled until the user selects something from the first box
    Using con As New SqlClient.SqlConnection
        con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & AppPath & "App_Data\DeviceDatabase.MDF;Integrated Security=True;User Instance=True;"
        Using cmd As New SqlClient.SqlCommand
            cmd.Connection = con
        End Using
        Dim cmdSQL As New SqlCommand()
        cmdSQL.CommandType = Data.CommandType.Text
        cmdSQL.CommandText = "SELECT Fieldname1 FROM " & """" & DropDownList1.SelectedItem.ToString & """" 'Im pretty sure this isnt right, and the reason I use """"" is because some of the items in the dropdown have spaced words.

        Dim adptSQL As New SqlClient.SqlDataAdapter(cmdSQL)
        Dim myDataSet As New DataSet()
        adptSQL.Fill(myDataSet)

        With myDataSet.Tables(DropDownList1.SelectedIndex) 'I think this is right
            For rowNumber As Integer = 0 To .Rows.Count - 1
                With .Rows(rowNumber)
                    DropDownList2.Items.Add(col1.rowNumber) 'This is obviously not working 
                End With
            Next
        End With
    End Using
End Sub

然后,我不太确定如何使用所选行填充数据表,尽管目前我可以使用以下方法完成整个表:

Private Sub GenTables(ByVal DropList As Object)
    If DropList.SelectedIndex = 0 Then
        GridView1.DataSourceID = Nothing
    ElseIf DropList.SelectedIndex = 1 Then
        GridView1.DataSourceID = "SqlDataSource1"
    ElseIf DropList.SelectedIndex = 2 Then
        GridView1.DataSourceID = "SqlDataSource2"
    ElseIf DropList.SelectedIndex = 3 Then
        GridView1.DataSourceID = "SqlDataSource3"
    ElseIf DropList.SelectedIndex = 4 Then
        GridView1.DataSourceID = "SqlDataSource4"
    End If
    GridView1.DataBind()
End Sub

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DeviceDatabaseConnectionString1 %>" 
        ProviderName="<%$ ConnectionStrings:DeviceDatabaseConnectionString1.ProviderName %>" 
        SelectCommand="SELECT [Device:] AS column1, [SWversion:] AS column2, [Date:] AS column3, [Tester:] AS column4, [Wifi Preferred InCov:] AS column5 FROM [Galaxy Nexus]">
    </asp:SqlDataSource>

'there are 3 more of these.

但是我将这些表“硬编码”到应用程序中,显然我不能对每个表行都这样做。那么如何在不提前在 asp 中设置 SQLDataSource 的情况下填充数据网格呢?

最后,单击按钮删除与数据网格中显示的信息相关的行。如果第一部分能得到一点帮助,我相信我能弄清楚第二部分。

所以我要问的是:如何用来自 Coloumn1 的所有项目填充下拉列表?以及如何从特定行填充数据网格?

任何和所有的帮助总是非常感谢。多谢你们

扎克

编辑

嗯,我想我让这件事变得更难了,现在我正在处理这个:

      Protected Sub BindDrop_Click(sender As Object, e As System.EventArgs)
        DropDownList2.DataSourceID = "SqlDataSource5"
        DropDownList2.DataBind()      
      End Sub

<asp:SqlDataSource ID="SqlDataSource5" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DeviceDatabaseConnectionString1 %>" 
        ProviderName="<%$ ConnectionStrings:DeviceDatabaseConnectionString1.ProviderName %>" 
        SelectCommand="SELECT [Device:] AS column1 FROM [Galaxy Nexus]">

它不太正确,但它更接近并且在 1/10 行

4

1 回答 1

0

好吧,伙计们,我想通了,我需要使用 ExecuteReader 函数(这太疯狂了,我在自动填充中找不到一篇使用此方法的文章)。希望在写/回答这个问题时,我会让某人的生活更轻松。

 Protected Sub DropDownList2_SelectedIndexChanged(sender As Object, e As System.EventArgs)
    DropDownList3.Enabled = True
    DropDownList3.Items.Clear()
    Dim newsqlcommand As String = "Select [SWversion:] FROM " & """" & DropDownList2.SelectedItem.ToString & """"       
    Using con As New System.Data.SqlClient.SqlConnection(connexstring)
        con.Open()
        Using cmd As New SqlCommand(newsqlcommand, con)
            Dim myReader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
            While myReader.Read()            
                DropDownList3.Items.Add(myReader.GetString(0))
            End While
            myReader.Close()
            cmd.Dispose()
        End Using
        con.Close()
        con.Dispose()
    End Using
    Dbind()    
End Sub   

这成功地读取了“SWVersion”列中的所有项目,并将它们添加到 dropdown3 的下拉列表中。享受!

于 2012-09-26T17:46:20.807 回答