Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

asp.net - accept post data in asp and insert into sql server

I just re did my question and added a few other scripts needed to pull everything together

This is the small script I am using to receive the post and push the data into the SQL Server (2008 if that matters): UPDATE 2 after suggestions:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myConn As SqlConnection = New SqlConnection("Integrated Security=false;Data Source=.;Initial Catalog=DOMAIN_NAME;UserID=user;Password=password")
myConn.Open()
Dim sqlstring As String = " INSERT INTO sean.local (etype, latitude, longtitude, phone) VALUES ('" + Request.Form("type") + "','" + Request.Form("latitude") + "','" + Request.Form("longtitude") + "','" + Request.Form("phone") + "')"
Dim cmd As SqlCommand = New SqlCommand(sqlstring, myConn)
cmd.ExecuteNonQuery()
myConn.Close()
Response.Redirect(Request.RawUrl, True)
Response.Write("1 record added")
End Sub
</script>

here is my create table script

CREATE TABLE local
(
P_Id int PRIMARY KEY IDENTITY,
etype varchar(255) NOT NULL,
latitude varchar(255),
longtitude varchar(255),
phone varchar(255)
)

and here is the form I am using to test

 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
 <title>A Web Page</title>
 </head>
 <body>
 <BR></BR>
 <form action="http://www.mydomain.com/script.asp" method="post">
 <h1>Form Test</h1>
 Phone:<BR></BR><input type="text" name="phone"/>      
 <BR></BR>
 type:<BR></BR><input type="text" name="type"/>
 <BR></BR>
 lat:<BR></BR><input type="text" name="latitude"/>
 <BR></BR>
 lng:<BR></BR><input type="text" name="longtitude"/>
 <BR></BR>
 <input type="submit" name="submit" value="Submit Data"/>
 </form>
 </body>
 </html>

It might be something small but I really have no other ideas...thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's mostly VB, just switch to it

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim myConn As SqlConnection = New SqlConnection("Integrated Security=false;Data Source=.;Initial Catalog=DOMAIN_NAME;UserID=abc;Password=123")
        myConn.Open()
        Dim sqlstring As String = " INSERT INTO sean.local (etype, latitude, longtitude, phone) VALUES ('" + Request.Form("type") + "','" + Request.Form("latitude") + "','" + Request.Form("longtitude") + "','" + Request.Form("phone") + "')"
        Dim cmd As SqlCommand = New SqlCommand(sqlstring, myConn)
        cmd.ExecuteNonQuery()
        myConn.Close()
        Response.Redirect(Request.RawUrl, True)
        Response.Write("1 record added")
    End Sub
</script>

If you need it in C#, run it through this: http://wiki.sharpdevelop.net/Code%20Converter.ashx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...