歡迎大家一起進來討論討論!
--------------------------------------------------------------------------------
using System.Data;
using System.Data.OleDb;
private OleDbConnection myConnection
private OleDbCommand myCommand
1.連接數據庫
string dbname = System.Web.HttpContext.Current.Server.MapPath("/App_Data/authors.mdb");
//注意:這裏之所以是/App_Date下級目錄是因爲作者使用的是Visual Studio 2005版本的
myConnection = New OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source="+dbname )
myConnection.Open()
lbl1.text="Connection Opened!"
2.添加記錄
myCommand = New OleDbCommand( "Insert INTO Authors(Authors,country) Values('Simson','usa')", myConnection )
myCommand.ExecuteNonQuery()
lbl2.text="New Record Inserted!"
3 更新數據(Access)
myCommand = New OleDbCommand( "UPDATE Authors SET Authors='Bennett' WHERE Authors = 'Simson'", myConnection )
myCommand.ExecuteNonQuery()
lbl3.text="Record Updated!"
4 刪除數據(access)
myCommand = New OleDbCommand( "DELETE FROM Authors WHERE Authors = 'David'", myConnection )
myCommand.ExecuteNonQuery()
lbl4.text="Record Deleted!"
5 使用DateGrid顯示數據
myCommand = New OleDbCommand( "select * FROM Authors", myConnection )
MyDataGrid.DataSource=myCommand.ExecuteReader()
MyDataGrid.DataBind()
<html>
<body>
<asp:label id="lbl1" runat="server" /><br>
<asp:label id="lbl2" runat="server" /><br>
<asp:label id="lbl3" runat="server" /><br>
<asp:label id="lbl4" runat="server" /><br>
<asp:DataGrid ID="MyDataGrid" runat="server" >
</asp:DataGrid>
</body>
</html>
如上述代碼有什麼問題請留言或聯繫我 謝謝 EMAIL:[email protected]