获取数据库表列?

时间:2012-01-12 10:08:47

标签: asp.net c#-4.0

嗨我在数据库中有一个表,我希望只将列标题绑定到下拉列表........

我曾尝试过以硬编码方式工作的方式 这是被删除的代码

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Data;
      using System.Web.UI;
      using System.Web.UI.WebControls;
       using System.Data.OleDb;

      namespace WebApplication1
     {
    public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string Excelpath = Server.MapPath("~/ExcelFiles/TaskSheet.xlsx");
        string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Excelpath + ";Extended Properties='Excel 12.0;HDR=YES;'";
        OleDbDataAdapter DB = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", new OleDbConnection(connString));

        System.Data.DataSet DS = new System.Data.DataSet();
        DB.Fill(DS);
        Table t = new Table();


        foreach (DataTable table in DS.Tables) { 
            foreach (DataColumn column in table.Columns)
            {
                DropDownList list = new DropDownList(); list.Items.Add("Name"); list.Items.Add("Date"); list.Items.Add("Task");
                Label l = new Label(); l.Text = column.ColumnName;
                TableRow r = new TableRow();
                TableCell c = new TableCell();
                c.Controls.Add(l);

                TableCell c1 = new TableCell();
                c1.Controls.Add(list);

                r.Cells.Add(c);
                r.Cells.Add(c1);
                t.Rows.Add(r);

            }
        }
        Page.Form.Controls.Add(t);
    }
}
}

<小时/> 而不是绑定每个和单个列我想循环

我试过这个


      public partial class WebForm1 : System.Web.UI.Page
          {
       protected void Page_Load(object sender, EventArgs e)
         {
          string Excelpath = Server.MapPath("~/ExcelFiles/TaskSheet.xlsx");
           string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +     Excelpath + ";Extended Properties='Excel 12.0;HDR=YES;'";
        OleDbDataAdapter DB = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", new OleDbConnection(connString));

        System.Data.DataSet DS = new System.Data.DataSet();
        DB.Fill(DS);
        Table t = new Table();


        foreach (DataTable table in DS.Tables) 
        { 
            foreach (DataColumn column in table.Columns)
            {
                string insertstring = @"select * from CUSTOMER_DETAILS1";
                SqlConnection conn = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DebitCare;Data Source=SSDEV7-HP\\SQLEXPRESS");
                conn.Open();
                SqlCommand cmd = new SqlCommand(insertstring, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet data = new DataSet();
                adapter.Fill(data);
                conn.Close();
                DropDownList list = new DropDownList();
                list.DataSource = data.Tables[0];
                list.Items.Add("data");
                list.DataBind();


                //ddlFrom.DataSource = data.Tables[0];
                //ddlFrom.DataValueField = "FromId";
                //ddlFrom.DataTextField = "From";
                //ddlFrom.DataBind();
               // DropDownList list = new DropDownList(); list.Items.Add("Name"); list.Items.Add("Date"); list.Items.Add("Task");
                Label l = new Label(); l.Text = column.ColumnName;
                TableRow r = new TableRow();
                TableCell c = new TableCell();
                c.Controls.Add(l);

                TableCell c1 = new TableCell();
                c1.Controls.Add(list);

                r.Cells.Add(c);
                r.Cells.Add(c1);
                t.Rows.Add(r);

            }
        }
        Page.Form.Controls.Add(t);
    }

}
}

我在这方面有任何错误,但我无法看到下拉列表中的cloumns ......任何人都可以帮助我

1 个答案:

答案 0 :(得分:1)

假设您正在使用SQL Server(大假设)

此SQL将为您提供可以绑定到下拉列表的表的列标题列表

select COLUMN_NAME from INFORMATION_SCHEMA.Columns
where Table_Name = 'MyTable'

将通过标题列表向您提供下拉列表 - 不确定是否会让您向前移动

修改

哎呀 - 你正在使用访问权限不是你。这是离INFORMATION_SCHEMA最近的数据库元数据

OleDbConnection.GetOleDbSchemaTable()

使用此代码获取列列表

cn.Open()

'Retrieve schema information about columns.
'Restrict to just the Employees TABLE.
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, _
              New Object() {Nothing, Nothing, "Your table", Nothing})

'List the column name from each row in the schema table.
For i = 0 To schemaTable.Rows.Count - 1
    Console.WriteLine(schemaTable.Rows(i)!COLUMN_NAME.ToString)
Next i

'Explicitly close - don't wait on garbage collection.
cn.Close()

我找到了here。对不起,它在VB.Net中 - 根据需要很容易转换为C#。