菜鸟级问题,如何用代码来bind GridView
{
OleDbConnection conn1 = new OleDbConnection( "Provider=MicroSoft.Jet.OLEDB.4.0;Data Source=D:\\Web1\\App_Data\\Authors.mdb ");
string strcmd1 = "select * from Authors ";
OleDbCommand cmd1 = new OleDbCommand(strcmd1, conn1);
try
{
conn1.Open();
//要让gridview1 中显示出数据来,下代这段代码该如何写,我不想用拖控件生成的代码来实现。
GridView1.DataSource ..
...;
}
finally
{
conn1.Close();
}
}
------解决方案--------------------{
OleDbConnection conn1 = new OleDbConnection( "Provider=MicroSoft.Jet.OLEDB.4.0;Data Source=D:\\Web1\\App_Data\\Authors.mdb ");
string strcmd1 = "select * from Authors ";
DataSet ds = new DataSet();
OleDbCommand cmd1 = new OleDbCommand(strcmd1, conn1);
try
{
conn1.Open();
//要让gridview1 中显示出数据来,下代这段代码该如何写,我不想用拖控件生成的代码来实现。
dap.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
finally
{
conn1.Close();
}
}
------解决方案-------------------- OleDbConnection cnn = new OleDbConnection( );
cnn.ConnectionString = "Your Connection String "
OleDbCommand cmd = new OleDbCommand( );
cmd.Connection = cnn;
string strSql = @ "
SELECT
*
FROM
Your Table
WHERE
Your Condition ";
cmd.CommandText = strSql;
DataTable dt = new DataTable( );
try
{
using ( OleDbDataAdapter adapter = new OleDbDataAdapter( cmd ) )
{
adapter.Fill( dt );
}
}
catch ( OleDbException ex )
{
throw ( ex );
}
finally
{
if ( cnn.State == ConnectionState.Open )
{
cnn.Close( );
}
}
YourGridView.DataSource = dt ;
YourGridView.DataBind();