none
ASP.NET怎么把SQL数据库里的所有表名绑定到下拉框?点击下拉框右边的打开按钮可以在gridview上显示选中的表!下面代码什么问题?怎么下拉框没绑定上? RRS feed

  • 问题

  • protected void Button1_Click1(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["manageConnectionString"].ConnectionString);
            con.Open();
            string sqlstr4 = "select * from " + DropDownList1.SelectedValue;
            SqlDataAdapter myada4 = new SqlDataAdapter(sqlstr4, con);
            DataSet myds4 = new DataSet();
            myada4.Fill(myds4);
            this.GridView1.DataSource = null;
            this.GridView1.DataSource = myds4.Tables[0];   
            this.GridView1.DataBind();
           }

    2011年10月31日 7:48

答案

  • 依照你所描述,我做了下列的測試程式碼,有興趣可參考看看,不知道是否符合你的需求。

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace WebApplication6
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DropDownList1.DataSource = GetData("select * from sys.tables");
                    DropDownList1.DataTextField = "name";
                    DropDownList1.DataBind();
                }
            }
    
            private static DataTable GetData(string sqlcmd)
            {
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                scsb.DataSource = ".";
                scsb.InitialCatalog = "Northwind";
                scsb.IntegratedSecurity = true;
    
                DataTable dtTables = new DataTable();
    
                using (SqlConnection con = new SqlConnection(scsb.ToString()))
                {
                    using (SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd, con))
                    {
                        adapter.Fill(dtTables);
                    }
                }
    
                return dtTables;
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                GridView1.DataSource = GetData(string.Format("select * from {0}", DropDownList1.SelectedItem.Text));
                GridView1.DataBind();
            }
        }
    }
    

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication6.WebForm2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
            <hr />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
            </asp:GridView>
        </div>
        </form>
    </body>
    </html>
    
    

     


    以上說明若有錯誤請指教,謝謝。
    http://www.dotblogs.com.tw/terrychuang/
    • 已标记为答案 深白 2011年11月1日 1:52
    2011年10月31日 9:01

全部回复

  • 断点调试sqlstr4,看看里边是什么内容,可以取到内容吗?


    如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    我的博客园
    慈善点击,点击此处
    2011年10月31日 8:09
  • 依照你所描述,我做了下列的測試程式碼,有興趣可參考看看,不知道是否符合你的需求。

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace WebApplication6
    {
        public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    DropDownList1.DataSource = GetData("select * from sys.tables");
                    DropDownList1.DataTextField = "name";
                    DropDownList1.DataBind();
                }
            }
    
            private static DataTable GetData(string sqlcmd)
            {
                SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                scsb.DataSource = ".";
                scsb.InitialCatalog = "Northwind";
                scsb.IntegratedSecurity = true;
    
                DataTable dtTables = new DataTable();
    
                using (SqlConnection con = new SqlConnection(scsb.ToString()))
                {
                    using (SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd, con))
                    {
                        adapter.Fill(dtTables);
                    }
                }
    
                return dtTables;
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                GridView1.DataSource = GetData(string.Format("select * from {0}", DropDownList1.SelectedItem.Text));
                GridView1.DataBind();
            }
        }
    }
    

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication6.WebForm2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server">
            </asp:DropDownList>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
            <hr />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
            </asp:GridView>
        </div>
        </form>
    </body>
    </html>
    
    

     


    以上說明若有錯誤請指教,謝謝。
    http://www.dotblogs.com.tw/terrychuang/
    • 已标记为答案 深白 2011年11月1日 1:52
    2011年10月31日 9:01