http://www.dotblogs.com.tw/mis2000lab/archive/2009/10/09/10980.aspx
'----自己寫的----
Imports System
Imports System.Web.Configuration
Imports System.Data
Imports System.Data.SqlClient
'----自己寫的----
Imports System
Imports System.Web.Configuration
Imports System.Data
Imports System.Data.SqlClient
'----自己寫的----
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Conn As SqlConnection = New SqlConnection
Conn.ConnectionString = WebConfigurationManager.ConnectionStrings("Web.Config檔案裡面 ConnectionString").ConnectionString
Conn.Open() '---- 連結DB
Dim Conn As SqlConnection = New SqlConnection
Conn.ConnectionString = WebConfigurationManager.ConnectionStrings("Web.Config檔案裡面 ConnectionString").ConnectionString
Conn.Open() '---- 連結DB
Dim dr As SqlDataReader = Nothing
Dim cmd As SqlCommand
cmd = New SqlCommand("
使用參數的寫法,可以避免一些攻擊:
cmd = New SqlCommand("select * from User資料表 where 帳號 = @id", Conn)
cmd.Parameters.AddWithValue("@id", TextBox2.Text)
dr = cmd.ExecuteReader() '---- 執行SQL指令,取出資料
'////////////////////////////////////////////////////////////////////////////////////////////////////////////////
If dr.HasRows() Then
Response.Write("Error~ 找到相同的帳號,所以程式停止!!")
Response.End()
Else '-- 沒有找到相同標題的文章,所以可以新增這筆資料 '-- 請自己撰寫資料新增的程式碼
Response.Write("新增資料成功!!")
End If
If Not (dr Is Nothing) Then
cmd.Cancel()
dr.Close()
End If
'---- Close the connection when done with it.
If (Conn.State = ConnectionState.Open) Then
Conn.Close()
Conn.Dispose()
End If
End Sub
沒有留言:
張貼留言