如何创建一个允许我选择要编辑的SQL表的下拉列表 [英] How do create a dropdownlist that allows me to choose a SQL table to edit

查看:68
本文介绍了如何创建一个允许我选择要编辑的SQL表的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL server express中有三个表是客户,问题,子问题我想要做的是能够通过从有3个选项的ddl(客户,问题,子)中选择从单个ASP页面编辑它们然后选择ddl会显示所选表中数据的gridview,并提供更新或删除行的选项。

我可以创建一个ddl,从SQL数据库sys中选择表.tables,但我需要使用所选名称作为变量来在网格视图中显示表的内容。因此,如果我从ddl中选择客户,则会显示customers表,我可以对其进行编辑,如果我选择问题,则会显示问题等。 - 任何想法感激不尽,欢呼Kevin。



SQL代码:

I have three table in SQL server express that are customers, problem, sub problems what I want to do is be able to edit them from a single ASP page by choosing from a ddl that has 3 choices (customers, problem, sub problem) and that choice of the ddl then displays a gridview of the data in the selected table with option to update or delete the rows.
I can create a ddl which selects the tables from the SQL database sys.tables, but I need to use the selected name as a variable to display the contents of the table in a grid view. so if I select customers from the ddl the customers table is displayed and I can edit it, if I select problem then problem is displayed etc.etc. - any ideas gratefully appreciated, cheers Kevin.

SQL code:

select T.name collate database_default as TableName
from Incident_Tracker.sys.tables as T where Name like 'Customers' or Name like 'problemType' or Name like 'problemsubType' or name like 'Products'or name like 'Sub_Products'





用于选择ddl的ASP代码:



ASP Code to select ddl:

Protected Sub Page_Load4(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
       If Not IsPostBack Then
           ddlSelectTable.AppendDataBoundItems = True
           Dim strQuery As [String] = "select T.name collate database_default as TableName from Incident_Tracker.sys.tables as T where Name like 'Customers' or Name like 'problemType' or Name like 'problemsubType' or name like 'Products'or name like 'Sub_Products'"
           sqlCon = New SqlConnection(strConn)
           Using (sqlCon)
               Dim sqlComm As New SqlCommand()
               sqlComm.CommandType = CommandType.Text
               sqlComm.CommandText = strQuery
               sqlComm.Connection = sqlCon
               Try
                   sqlCon.Open()
                   ddlSelectTable.DataSource = sqlComm.ExecuteReader()
                   ddlSelectTable.DataTextField = "TableName"
                   ddlSelectTable.DataValueField = "TableName"
                   ddlSelectTable.DataBind()
               Catch ex As Exception
                   Throw ex
               Finally
                   sqlCon.Close()
                   sqlCon.Dispose()
               End Try
           End Using
       End If
   End Sub







为带有网格视图的页面添加了代码 - 问题是我无法将变量从ddl选择传递到网格视图的select语句,然后获取网格视图以从select语句的结果中自动生成列。那么我希望能够根据需要在网格视图中编辑或删除行。

下面是aspx页面的代码。






Added code for page with grid view - the issue is I cant pass the variable from the ddl selection into the select statement for the grid view, then get the grid view to auto generate the columns from the results of the select statement. then I want to be able to edit or delete rows in the grid view as required.
Below code for aspx page.

<pre lang="HTML"><pre lang="HTML"><pre lang="HTML">

<%@ Page Title =Langua ge =vbAutoEventWireup =falseMasterPageFile =〜/ Site.MasterCodeBehind =managecustomers.aspx.vbInherits =WebApplication4.managecustomers%>

< asp: Content ID =Content1ContentPlaceHolderID =HeadContentrunat =server>



< asp:Content ID =Content2ContentPlaceHolderID =FeaturedContentrunat =server>



< asp:Content ID =Content3ContentPlaceHolderID =MainContentrunat =server>

< asp:UpdatePanel ID =UpdatePanel1runat =server>

< contenttemplate>

< asp:DropDownList ID =ddlSelectTablerunat =server>









< asp:GridView ID =GridView1runat =serverAutoGenerateColumns =FalseDataKeyNames =Customer_IDDataSourceID =SqlDataSource1>

< columns> < asp:CommandField ShowDeleteButton =TrueShowEditButton =True/>

< asp:BoundField DataField =Customer_IDHeaderText =Customer_IDReadOnly =TrueSortExpression =Customer_ID />

< asp:BoundField DataField =Customer_NameHeaderText =Customer_NameSortExpression =Customer_Name/>

< asp:BoundField DataField = Customer_AccountNoHeaderText =Customer_AccountNoSortExpression =Customer_AccountNo/>









< asp:SqlDataSource ID =SqlDataSource1runat =serverConnectionString =<%$ ConnectionStrings:Incident_TrackerConnectionString%> DeleteCommand =DELETE FROM [@TableName] WHERE [Customer_ID] = @Customer_IDSelectCommand =SELECT * FROM [@TableName]UpdateCommand =UPDATE [@TableName] SET [Customer_Name] = @Customer_Name,[Customer_AccountNo] = @Customer_AccountNo WHERE [Customer_ID] = @Customer_ID>

< deleteparameters>

< asp:参数名称=Customer_ID类型=对象/>



< updateparameters>

< asp:参数名称=Customer_NameType =String/>

< asp:参数名称=Customer_AccountNoType =String/>

< asp:参数名称=Customer_ID类型=对象/>













谢谢。

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeBehind="managecustomers.aspx.vb" Inherits="WebApplication4.managecustomers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">

<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<contenttemplate>
<asp:DropDownList ID="ddlSelectTable" runat="server" >





<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Customer_ID" DataSourceID="SqlDataSource1">
<columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="Customer_ID" HeaderText="Customer_ID" ReadOnly="True" SortExpression="Customer_ID" />
<asp:BoundField DataField="Customer_Name" HeaderText="Customer_Name" SortExpression="Customer_Name" />
<asp:BoundField DataField="Customer_AccountNo" HeaderText="Customer_AccountNo" SortExpression="Customer_AccountNo" />





<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Incident_TrackerConnectionString %>" DeleteCommand="DELETE FROM [@TableName] WHERE [Customer_ID] = @Customer_ID" SelectCommand="SELECT * FROM [@TableName]" UpdateCommand="UPDATE [@TableName] SET [Customer_Name] = @Customer_Name, [Customer_AccountNo] = @Customer_AccountNo WHERE [Customer_ID] = @Customer_ID">
<deleteparameters>
<asp:Parameter Name="Customer_ID" Type="Object" />

<updateparameters>
<asp:Parameter Name="Customer_Name" Type="String" />
<asp:Parameter Name="Customer_AccountNo" Type="String" />
<asp:Parameter Name="Customer_ID" Type="Object" />






Thanks.

推荐答案

ConnectionStrings:Incident_TrackerConnectionStrin g%>DeleteCommand =DELETE FROM [@TableName] WHERE [Customer_ID] = @Customer_IDSelectCommand =SELECT * FROM [@TableName]UpdateCommand =UPDATE [@TableName] SET [Customer_Name] = @Customer_Name,[ Customer_AccountNo] = @Customer_AccountNo WHERE [Customer_ID] = @Customer_ID>

< deleteparameters>

< asp:参数名称=Customer_IDType =Object />



< updateparameters>

< asp:参数名称=Customer_NameType =String/>

< asp:参数名称=Customer_AccountNoType =String/>

< asp:参数名称=Customer_ID类型=对象/> ;













谢谢。
ConnectionStrings:Incident_TrackerConnectionString %>" DeleteCommand="DELETE FROM [@TableName] WHERE [Customer_ID] = @Customer_ID" SelectCommand="SELECT * FROM [@TableName]" UpdateCommand="UPDATE [@TableName] SET [Customer_Name] = @Customer_Name, [Customer_AccountNo] = @Customer_AccountNo WHERE [Customer_ID] = @Customer_ID">
<deleteparameters>
<asp:Parameter Name="Customer_ID" Type="Object" />

<updateparameters>
<asp:Parameter Name="Customer_Name" Type="String" />
<asp:Parameter Name="Customer_AccountNo" Type="String" />
<asp:Parameter Name="Customer_ID" Type="Object" />






Thanks.


这篇关于如何创建一个允许我选择要编辑的SQL表的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆