Add a record to SQL in asp+

Source: Internet
Author: User
Tags insert
asp+


This article will show you how to record your home on a SQL database in asp+.
Assuming you've mastered the basics of the database,
Ok
We make an Instance database demo table Sqladd, a field name (char)
We'll explain how to add data based on this database.
Now let's start coding.
First, we make a add.htm page (this is the page that submits the form)
<body>
  
<form method= "POST" action= "sql-add.aspx" >
<p>name: <input type= "text" name= "Name" size= "></p>
<p>
<input type= "Submit" value= "ADD" name= "B1" >
<input type= "reset" value= "Start-over" name= "B2" ></p>
</form>
  
</body>
In the second step, we will make the sql-add.aspx to save the data to the database page.
We will add the appropriate namespaces and connection strings (specify your server, username and password)
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SQL"%>
  
<HTML>
<BODY>
<script language= "VB" runat= "Server" >
  
Sub Page_Load (Src as Object, E as EventArgs)
  
Dim MyConnection as SqlConnection
myconnection = new SqlConnection ("Server=localhost; Uid=sa; pwd=; Database=demo ")
  
Here we will declare mycommand as the SqlCommand variable to execute the INSERT statement
  
Dim MyCommand as SQLCommand
mycommand = new SQLCommand ("INSERT into Sqladd (name) VALUES ('" & Request.Form ("name") & ")",
  
MyConnection)
  
Finally, we open the database connection, execute the query, close the connection, the operation completed. The data is inserted into the database!
  
Myconnection.open ()
Mycommand.execute ()
Myconnection.close ()
  
Response.Write ("Record added successfully!") ")
  
End Sub
  
</script>
  
</BODY>
</HTML>
  
Below, show the complete source code.
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SQL"%>
  
<HTML>
<title> Welcome to use chenyangasp Sample files </title>
<BODY>
<script language= "VB" runat= "Server" >
  
Sub Page_Load (Src as Object, E as EventArgs)
  
Dim MyConnection as SqlConnection
myconnection = new SqlConnection ("Server=localhost; Uid=sa; pwd=; Database=sean ")
  
Dim MyCommand as SQLCommand
mycommand = new SQLCommand ("INSERT into Sqladd (name) VALUES ('" & Request.Form ("name") & ")",
  
MyConnection)
  
Myconnection.open ()
Mycommand.execute ()
Myconnection.close ()
  
Response.Write ("Record added successfully!") ")
  
End Sub
  
</script> </HTML>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.