【问题标题】:accept post data in asp and insert into sql server接受asp中的post数据并插入sql server
【发布时间】:2012-03-27 07:34:31
【问题描述】:

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

这是我用来接收帖子并将数据推送到 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>

这可能有点小,但我真的没有其他想法……谢谢。

【问题讨论】:

  • “我想我正在将 VB 和 C# 混合在一起,我只是纠结和迷失,” - 以前从未听说过!
  • Dim varname As Type 在 C# 中应该是 Type varname;
  • 您的查询容易受到 sql 注入攻击。不要使用这样的字符串连接来构建查询。
  • 我会调查一下,我真的很想先获得基本功能,然后再对我的数据进行清理......你对我缺少什么有任何想法吗?

标签: asp.net sql vb.net post


【解决方案1】:

这将解决您现有代码中的几个问题,包括一些您甚至还不知道的问题:

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

【讨论】:

  • 谢谢 Joel,我仍然收到 500 错误,我正在尝试查看是否可以从 IE 或 Firefox 获得任何其他反馈,但我无法弄清楚
【解决方案2】:

主要是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

【讨论】:

  • 所以在托管方面一切正常...我正在调查 SQL 服务器
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多