使用Visual Studio 2010我创建了一个VB Web应用程序,我添加了一个数据库。我有一个VB类,它假设取一个名称并通过存储过程将其添加到数据库中。我运行代码并将变量传递给vb类,但是一旦我停止运行程序,似乎没有更改表。
我是vb和视觉工作室的新手,所以我使用http://www.macronimous.com/resources/calling_stored_procedures_from_ASP.NET_and_VB.NET.asp作为我的向导,这就是代码就是这样的原因。
这是vb类
Imports System.Data
Imports System.Data.SqlClient
Public Class human
Private name As String
Private id As Integer
Public Function sendName(ByVal nm As String)
Dim SQLCon As New SqlClient.SqlConnection
Dim SQLCmd As New SqlCommand
SQLCon.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=
C:\Documents and Settings\user\my documents\visual studio 2010\Projects\
WebApplication3\WebApplication3\App_Data\Database1.mdf;
Integrated Security=True;User Instance=True"
SQLCon.Open()
'I want to to execute the procedure addName using the variable nm
SQLCmd.CommandText = "addName"
SQLCmd.CommandType = CommandType.StoredProcedure
SQLCmd.Parameters.AddWithValue("@name", nm)
SQLCmd.Connection = SQLCon 'Active Connection
SQLCon.Close()
Return (nm)
End Function
End Class
and here's my stored procedure "addName"
Alter PROCEDURE addName (@GivenName varchar(100))
AS
BEGIN
SET NOCOUNT ON;
Insert into tableA (name) values (@GivenName);
END
return
在我停止调试应用程序后,我错过了什么阻止表显示更新?
答案 0 :(得分:4)
大。您定义一个命令,您创建一个连接并打开它。但是你无法执行命令。
SQLCmd.Connection = SQLCon 'Active Connection
SQLCmd.ExecuteNonQuery()
SQLCon.Close()
答案 1 :(得分:2)
您省略了语句SQLCmd.ExecuteNonQuery()
SQLCmd.ExecuteNonQuery() 'You need this line
SQLCon.Close()
答案 2 :(得分:2)
你错过了ExecuteNonQuery()方法。
SQLCon.Open()
SQLCmd.CommandText = "addName"
SQLCmd.CommandType = CommandType.StoredProcedure
SQLCmd.Parameters.AddWithValue("@name", nm)
SQLCmd.Connection = SQLCon 'Active Connection
SQLCmd.ExecuteNonQuery() 'This one is missing
SQLCon.Close()