jinsheng1000 - 2005-7-28 20:08:00
C#的,连接的是access数据库!
string h = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = ";
String strConn = h + Server.MapPath("BBS.mdb");
OleDbConnection Conn = new OleDbConnection(strConn);
string sql = "select * from BBS";
OleDbDataAdapter Comm = new OleDbDataAdapter( sql,Conn);
DataSet ds = new DataSet();
Comm.Fill(ds,"BBS");
MyDataGrid.DataSource = ds.Tables["BBS"].DefaultView;
MyDataGrid.DataBind();
风云一号 - 2005-7-28 21:53:00
有什么不明白的吗?
这里有一个连SQL server 的例子!
只是为了说明方便! 建议最好连接串放web.config文件或加密到系统注册表中
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
private void Page_Load(object sender, System.EventArgs e)
{
if ( ! this.IsPostBack )
this.BindData();
}
protected void BindData()
{
SqlCommand cmd = new SqlCommand( "SELECT TOP 10 ProductID, ProductName FROM Products", con("Server=localhost; DataBase=Northwind; User Id=sa; Password=sa"));
this.DataGrid1.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
this.DataGrid1.DataBind();
}
protected SqlConnection con(System.String ConnectionString )
{
SqlConnection c = new SqlConnection( ConnectionString );
c.Open();
return c;
}
火麟蚀日 - 2005-7-31 12:18:00
更正一下,这个是.net连接Access的
清风阁☆四少 - 2005-8-1 23:31:00
string h = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = ";
String strConn = h + Server.MapPath("BBS.mdb");
//声明设置数据库连接字符串
OleDbConnection Conn = new OleDbConnection(strConn);
//实例化Conn并引用连接字符串
string sql = "select * from BBS";
//定义SQL命令字符串
OleDbDataAdapter Comm = new OleDbDataAdapter( sql,Conn);
//建立数据适配器并引用SQL命令和数据连接
DataSet ds = new DataSet();
//建立数据集
Comm.Fill(ds,"BBS");
//用Comm填充数据集
MyDataGrid.DataSource = ds.Tables["BBS"].DefaultView;
//DataGrid连接数据源
MyDataGrid.DataBind();
//数据绑定
明白了吧
加一句:次问题已结贴!
© 2000 - 2024 Rising Corp. Ltd.