這是我在數據表中插入數據的代碼。當Execute
我得到消息「成功」。但數據不在數據表中。使用vb.net從Sql數據庫插入,更新和刪除數據?
Imports System.Data
Imports System.Data.SqlClient
Partial Public Class _Default
Inherits System.Web.UI.Page
Dim s As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=G:\ANDY\Dot Net.Practical\SQL DATA BASE\inserting4\inserting4\App_Data\location.mdf;Integrated Security=True;User Instance=True"
Dim sql As String
Dim con As New SqlConnection
Dim cmd As SqlCommand
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
con = New SqlConnection(s)
con.Open()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
sql = "insert into location(name,street,city)values(@name,@street,@city)"
cmd = New SqlCommand(sql, con)
cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = TextBox1.Text
cmd.Parameters.Add("@street", SqlDbType.NVarChar).Value = TextBox2.Text
cmd.Parameters.Add("@city", SqlDbType.NVarChar).Value = TextBox3.Text
cmd.ExecuteNonQuery()
MsgBox("SUCCESS")
End Sub
End Class
你缺少前'ExecuteNonQuery'一個開放的連接字符串 – Marek
[在C#中插入問題,使用sqlcommand]的可能重複(http://stackoverflow.com/questions/3317868/insert-problem-in-c-using-sqlcommand) –