2012-03-09 44 views
-2

我只是重新做了我的問題,並增加了一些其他腳本需要拉都在一起接受ASP後的數據,並插入到SQL Server

這是我使用接收後,推動數據的小腳本到SQL服務器(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) 
) 

,這裏是在F orm我正在測試

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

它可能是一些小事,但我真的沒有其他的想法......謝謝。

+1

「我想我是在混合使用VB和C#,我只是糾結而迷失了,」 - 從來沒有聽說過這個! – 2012-03-09 21:12:10

+0

'Dim varname As Type'應該是'Type varname;'用C#。 – doogle 2012-03-09 21:15:59

+1

你的查詢很容易受到SQL注入攻擊。不要像那樣使用字符串連接來構建查詢。 – 2012-03-10 02:54:42

回答

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

+0

因此,一切看起來良好的託管端...我正在查看SQL服務器 – smithseanp16 2012-03-09 21:46:00

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

感謝喬爾,我仍然收到500錯誤,我想看看我是否可以從IE或Firefox獲得任何其他反饋,但我是無法弄清楚 – smithseanp16 2012-03-10 04:38:08