2012-01-12 76 views
1

嗨,我有一個表在數據庫中的一些列,我想只綁定列標題到下拉列表........獲取數據庫表中的列?

我曾嘗試過的方式,這是艱難的工作方式代碼 這是其excueted

 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 ......任何一個可以幫助我

+0

@Steve B就恰如沒有ü編輯 – 2012-01-12 10:26:22

+0

小修正了更好的代碼格式。你可以通過點擊「編輯XX分鐘前」 – 2012-01-12 10:33:35

回答

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#非常簡單。

+0

看到確切的變化,非常感謝budyyy – 2012-01-12 10:49:20

相關問題