こんにちは私はいくつかの列を持つデータベースにテーブルを持っています。私はドロップダウンリストに列ヘッダーだけをバインドしたいです.......データベーステーブルの列を取得しますか?
ハードな方法で作業していました私はこの
を試み
ことループするコード これは、各結合の代わり
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);
}
}
}
をexcuetedされるコード及び単一列であります
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);
}
}
}
私はこの内の任意のエラーを持っているDNTが、私はドロップダウンリストでcloumnsを参照してくださいカント...いずれも(ビッグ仮定)
このSQLは、SQL Serverを使用していると仮定すると、私に
@Steve B何を編集しましたか –
より良いコードの書式設定のための小さな修正。あなたは "編集されたXX分前"をクリックして正確な変更を見ることができます –