用ado+来删除数据

1@ Import Namespace="System.Data" 
1@ Import Namespace="System.Data.SQL" 
 1<html>
 2<script language="VB" runat="server">
 3
 4Dim MyConnection As SQLConnection 
 5
 6Sub Page_Load(Src As Object, E As EventArgs) 
 7
 8MyConnection = New SQLConnection("server=YOUR-SERVER;uid=joeuser;pwd=joeuser;database=pubs") 
 9
10If Not (IsPostBack)   
11BindGrid()   
12End If   
13End Sub 
14
15Sub MyDataGrid_Delete(Sender As Object, E As DataGridCommandEventArgs) 
16
17Dim MyCommand As SQLCommand   
18Dim DeleteCmd As String = "DELETE from Authors where au_id = @Id" 
19
20MyCommand = New SQLCommand(DeleteCmd, MyConnection)   
21MyCommand.Parameters.Add(New SQLParameter("@Id", SQLDataType.VarChar, 11))   
22MyCommand.Parameters("@Id").Value = MyDataGrid.DataKeys(CInt(E.Item.ItemIndex)) 
23
24MyCommand.ActiveConnection.Open() 
25
26Try   
27MyCommand.ExecuteNonQuery()   
28Message.InnerHtml = "<b>Record Deleted</b><br>" & DeleteCmd   
29Catch Exp As SQLException   
30Message.InnerHtml = "ERROR: Could not delete record"   
31Message.Style("color") = "red"   
32End Try 
33
34MyCommand.ActiveConnection.Close() 
35
36BindGrid()   
37End Sub 
38
39Sub BindGrid() 
40
41Dim DS As DataSet   
42Dim MyCommand As SQLDataSetCommand   
43MyCommand = New SQLDataSetCommand("select * from Authors", MyConnection) 
44
45DS = new DataSet()   
46MyCommand.FillDataSet(DS, "Authors") 
47
48MyDataGrid.DataSource=DS.Tables("Authors").DefaultView   
49MyDataGrid.DataBind()   
50End Sub 
51
52</script>
53<body style="font: 10pt verdana">
54<form runat="server">
55<h3><font face="Verdana">Deleting a Row of Data</font></h3>
56<span id="Message" maintainstate="false" runat="server" style="font: arial 11pt;"></span><p>
57<asp:datagrid backcolor="#ccccff" bordercolor="black" cellpadding="3" cellspacing="0" datakeyfield="au_id" font-name="Verdana" font-size="8pt" headerstyle-backcolor="#aaaadd" id="MyDataGrid" ondeletecommand="MyDataGrid_Delete" runat="server" showfooter="false" width="800">
58<property name="Columns">
59<asp:buttoncolumn commandname="Delete" text="Delete Author"></asp:buttoncolumn>
60</property>
61</asp:datagrid>
62</p></form>
63</body>
64</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus