开发者

I want to display the mysql table's contents into gridview

what can I do when my all database table's name into dropdown list & when i select or click the table name from dropdown list display the whole field into the 开发者_StackOverflowgridview dynamically..please give me best solution in asp.net.


Use the mysql commands SHOW TABLES and DESCRIBE table_name and draw the result into grids. I don't know the ASP codes.


(This is an example for MSSQL db. For mysql you can try using OdbcConnection or OleDbConnection depending on MySql version.)

1.Here is direct way to select data:

public class DAL
{
     public static DataTable GetTableData(string tableName)
        {
            DataTable dt = new DataTable();
            using (SqlConnection cn = new SqlConnection(Settings.Default.ConnectionString))
            {
                cn.Open();
                SqlCommand cmd = cn.CreateCommand();
                cmd.CommandText = string.Format("select * from {0}", tableName);
                using (SqlDataReader rd = cmd.ExecuteReader())
                {
                    bool ColumnsAdded = false;
                    while (rd.Read())
                    {
                        if (!ColumnsAdded)
                        {
                            for (int i = 0; i < rd.FieldCount; i++)
                                dt.Columns.Add(rd.GetName(i), rd.GetFieldType(i));
                        }
                        DataRow row = dt.NewRow();
                        for (int i = 0; i < rd.FieldCount; i++)
                            row[i] = rd[i];
                        dt.Rows.Add(row);
                        ColumnsAdded = true;
                    }
                }
            }
            return dt;
        }
}

2.Next you drop ObjectDataSource onto your form with a GridView (setting its DataSourceID to objectdatasource). Specify this method on your ObjectDataSource select method. You can specify parameter for tableName to be read from Control (DropDownList1 for instance). DropDownList1 presumebly keeps a list of tables.


You can dynamically load the table contents into gridview by putting a select query for the table and then bind that dataset to gridview datasource.

Hope this helps you!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜