<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>

<html>

<script language="VB" runat="server">

Dim myConnection As OleDbConnection
Dim myCommand    As OleDbDataAdapter
Dim ds           As New DataSet
Dim ConnStr      As String
Dim SQL          As String

Sub Page_Load(Source As Object, E As EventArgs) 
   ConnStr = "Provider=SQLOLEDB; Data Source=(local); Initial Catalog=ASPNET;User ID=sa;"
   myConnection = New OleDbConnection(ConnStr)      

    if NOT (isPostBack)
	rebind
    end if	
End Sub

Sub Show_Click(Sender As Object, E As EventArgs)
    Message.Text = "Masters Table Displayed... "
    ReBind
End Sub

Sub Insert_click(Sender As Object, E As EventArgs)
	    try
		sql = "Insert into Masters(code_display,code_category,type)"
		sql = sql + "Values ('test',701,'E')"
		Dim mycommand As New OleDbCommand(sql,myConnection)
		myConnection.Open()
		myCommand.ExecuteNonQuery()
		myConnection.Close()
	    catch myException as Exception
		Response.Write("出现异常: " + myException.ToString()) 
	    end try
	    rebind
	    Message.Text = "已插入记录...  " 
End Sub

Sub Delete_click(Sender As Object, E As EventArgs)
	    sql = "delete from masters where code_display = 'test'"
	    Dim mycommand As New OleDbCommand(sql,myConnection)
	    myConnection.Open()
	    myCommand.ExecuteNonQuery()
	    myConnection.Close()
	    rebind
	    Message.Text = "记录已删除..." 
End Sub

Sub Update_Click(Sender As Object, E As EventArgs)

	    try
	     Dim mycommand As New OleDbCommand( _
	      "更新 Masters 表，将code_display 为test的记录 Opening 设置为 90",  _
	       myConnection)
	     myConnection.Open()
	     myCommand.ExecuteNonQuery()
	     myConnection.Close()

            catch myException as Exception
		Response.Write("Exception: " + myException.ToString()) 
            end try
	    rebind
	    Message.Text = "已更新记录：将 closing balance 设为 90...! " 
End Sub

Sub ReBind()


  
    SQL = "select * from Masters"
    myCommand = New OleDbDataAdapter(SQL, myConnection)

    '使用Fill来填充 dataset
    myCommand.Fill(ds, "Masters")

    '绑定一个 DataGrid	
    DataGrid1.DataSource=ds.Tables("Masters").DefaultView
    DataGrid1.DataBind()

End Sub


</script>

<body>

<h3><font face="Verdana">操作查询</font></h3>


<form  runat=server>

 <asp:button text="刷新" Onclick="Show_Click" runat=server/>
 <asp:button text="插入" Onclick="Insert_Click" runat=server/>
 <asp:button text="更新" Onclick="Update_Click" runat=server/>
 <asp:button text="删除" Onclick="delete_Click" runat=server/>
 <asp:label id="Message"  runat=server/>
 <asp:DataGrid id="DataGrid1" runat="server" /> 


</form>

</body>

</html>

