接受asp中的post数据并插入sql server

时间:2012-03-09 21:09:29

标签: asp.net sql vb.net post

我刚刚回答了我的问题,并添加了一些其他所需的脚本来将所有内容整合在一起

这是我用来接收帖子并将数据推送到SQL Server的小脚本(如果重要的话,可以使用2008): 建议后更新2:

<%@ 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>

这是我的创建表脚本

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

这是我用来测试的表单

 <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>

它可能很小但我真的没有其他想法......谢谢。

2 个答案:

答案 0 :(得分:0)

主要是VB,只需切换到它

<%@ 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>

如果您需要在C#中使用它,请执行以下操作:http://wiki.sharpdevelop.net/Code%20Converter.ashx

答案 1 :(得分:0)

这将修复现有代码中的一些问题,包括一些您甚至还不知道的问题:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim sql As String = "INSERT INTO sean.local (etype, latitude, longtitude, phone) VALUES (@etype, @latitude, @longtitude, @phone)"
    Using myConn As New SqlConnection("Integrated Security=false;Data Source=.;Initial Catalog=DOMAIN_NAME;UserID=user;Password=password"), _
          cmd As New SqlCommand(sql, cn)

       'I had to make up database types... edit this to use the real types
       cmd.Parameters.Add("@etype", SqlDbType.Integer).Value = Request.Form("type")
       cmd.Parameters.Add("@latitude", SqlDbType.Float).Value = Request.Form("latitude")
       cmd.Parameters.Add("@longtitude", SqlDbType.Float).Value = Request.Form("longtitude")
       cmd.Parameters.Add("@phone", SqlDbType.Varchar, 12).Value = Request.Form("phone")

       myConn.Open()
       cmd.ExecuteNonQuery()
    End Using

    Response.Redirect(Request.RawUrl, True)

    'This line will never run!
    Response.Write("1 record added")

End Sub