Asp.net| Basic Tutorials | Tutorials |sql Database
The following code demonstrates how to use ASP.net to connect to a SQL server2000 database and manipulate code instances to share with asp.net beginners.
asp.net basic tutorial, connecting SQL Server2000 database Instance Code analysis:
The following is the code content:
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<script laguage= "VB" runat= "Server" >
Sub Page_Load (sender as object,e as EventArgs)
Dim MyConnection as SqlConnection
Dim MyCommand as SqlCommand
Dim DS as DataSet
' 1.connect to SQL Server
MyConnection = New SqlConnection ("Server=localhost;database=pubs;uid=ueytjdf;pwd=doekdf")
Myconnection.open ()
la1.text= "Connection opened!"
' 2.Create a table
mycommand = New SqlCommand ("CREATE TABLE [test] ([ID] [int] IDENTITY (1, 1) not NULL, [name]
[Char] (a) COLLATE chinese_prc_ci_as null, [sex] [char] (a) COLLATE chinese_prc_ci_as null
) ", MyConnection)
Mycommand.executenonquery ()
la2.text= "New table created!"
' 2 Add record
mycommand = New SqlCommand ("Insert into [test] (Name,sex) VALUES (' Huang Zhiwen ', ' Men ')",
MyConnection)
Mycommand.executenonquery ()
la3.text= "New record inserted!"
' 3 Update data
mycommand = New SqlCommand ("UPDATE [test] SET name= ' Smith ' where Name= ' Li Ming '", MyConnection)
Mycommand.executenonquery ()
la4.text= "Record updated!"
' 4 Delete data
mycommand = New SqlCommand ("delete from [test] where name= ' Smith '", MyConnection)
Mycommand.executenonquery ()
la5.text= "Record deleted!"
' 5 displaying Data in a DataGrid
mycommand = New SqlCommand ("SELECT * FROM [Test]", MyConnection)
Mydatagrid.datasource=mycommand.executereader ()
Mydatagrid.databind ()
End Sub
</script>
<body>
<asp:label id= "LA1" runat= "Server"/><br>
<asp:label id= "LA2" runat= "Server"/><br>
<asp:label id= "LA3" runat= "Server"/><br>
<asp:label id= "LA4" runat= "Server"/><br>
<asp:label id= "LA5" runat= "Server"/><br>
<asp:datagrid id= "Mydatagrid" runat= "Server"
Bordercolor= "BLACK"
Borderwidth= "1"
Gridlines= "Both"
Cellpadding= "3"
cellspacing= "0"
Font-name= "Verdana"
Font-size= "10pt"
Headerstyle-backcolor= "#aaaadd"
Alternatingitemstyle-backcolor= "#eeeeee"
>
</asp:DataGrid>
</body>
--> |