删除commad不能正常工作VB.Net [英] Delete commad is not working VB.Net
问题描述
嘿我已经在SQL中创建了一个存储过程,用于从我的数据库中删除数据,当我在SQL中使用它时,存储过程正常工作但是当我在VB.Net项目中调用相同的存储过程时,我在这里删除msg数据但在检查物理数据时仍保留在数据库中。任何人都可以提供帮助。
商店程序是
创建 proc sptblTrg_detail_Delete
@ Course_Id int
as
begin
delete tblTrg_Detail 其中 Trg_Ind_ID = @ Course_Id
结束
和VB.Net代码
私有 Sub BtnDelete_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs)句柄 BtnDelete.Click
Gp1.Enabled = True
如果 TxtPIS.Text = 然后
MsgBox( 请输入PIS否和正确批次否)
TxtPIS.Focus()
退出 Sub
其他
Dim Mycon 作为 新 SqlConnection(con)
Mycon.Open()
Dim da As SqlDataAdapter
da = 新 SqlDataAdapter( sptblTrg_d etail_Delete,Mycon)
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.SelectCommand.Parameters.AddWithValue( < span class =code-string> @ Course_Id,TxtCourseID.Text)
MsgBox( 详细信息已成功删除)
clearall()
Dgvtrainees.DataSource = 没什么
结束 如果
结束 Sub
这就是你需要的我认为
私有 Sub BtnDelete_Click( ByVal sender As System。 Object , ByVal e As System.EventArgs)句柄 BtnDelete.Click
如果 TxtPIS.Text = 然后
MsgBox( 请输入PIS否和正确批次NO)
TxtPIS.Focus()
退出 < span class =code-keyword> Sub
Else
Dim con 作为 字符串,RecordsAffected 作为 整数
尝试
使用 Mycon As 新 SqlConnection(con)
Mycon.Open()
使用 cmd 作为 新 SqlCommand( < span class =code-string> sptblTrg_detail_Delete,Mycon)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue( @ Course_Id,TxtCourseID.Text)
RecordsAffected = cmd.ExecuteNonQuery()
结束 使用
结束 使用
Catch ex As Exception
MessageBox.Show(ex 。消息)
最后
如果 RecordsAffected> 0 然后
MessageBox.Show( 详细信息已成功删除)
clearall()
Dgvtrainees.DataSource = Nothing
Else
MessageBox.Show( 删除问题)
结束 如果
结束 尝试
结束 如果
结束 Sub
嗨Ram,
您还没有写入语句来执行存储过程。 ..
尝试这个.. :)
1)你没有在存储过程中表现出来。
create proc sptblTrg_detail_Delete
@ Course_Id int
as
开始
删除 来自 tblTrg_Detail 其中 Trg_Ind_ID = @ Course_Id
结束
2)没有必要使用Sqldataadapter来删除数据库中的记录,你可以使用ExecuteNonQuery将其加密
hey I have have created a store procedure in SQL for deleting a data from my data base the store procedure is working fine when I am using it in SQL but when I call same store procedure in my VB.Net project here I am getting msg data deleted but when checking physically data still remains in data base . Can any one help.
The Store procedure is
create proc sptblTrg_detail_Delete @Course_Id int as begin delete tblTrg_Detail where Trg_Ind_ID= @Course_Id End
And VB.Net code
Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click Gp1.Enabled = True If TxtPIS.Text = "" Then MsgBox("Please Enter PIS No and Correct Batch NO") TxtPIS.Focus() Exit Sub Else Dim Mycon As New SqlConnection(con) Mycon.Open() Dim da As SqlDataAdapter da = New SqlDataAdapter("sptblTrg_detail_Delete", Mycon) da.SelectCommand.CommandType = CommandType.StoredProcedure da.SelectCommand.Parameters.AddWithValue("@Course_Id", TxtCourseID.Text) MsgBox("Detail Deleted Successfully") clearall() Dgvtrainees.DataSource = Nothing End If End Sub解决方案This is what you need i think
Private Sub BtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click If TxtPIS.Text = "" Then MsgBox("Please Enter PIS No and Correct Batch NO") TxtPIS.Focus() Exit Sub Else Dim con As String, RecordsAffected As Integer Try Using Mycon As New SqlConnection(con) Mycon.Open() Using cmd As New SqlCommand("sptblTrg_detail_Delete", Mycon) cmd.CommandType = CommandType.StoredProcedure cmd.Parameters.AddWithValue("@Course_Id", TxtCourseID.Text) RecordsAffected = cmd.ExecuteNonQuery() End Using End Using Catch ex As Exception MessageBox.Show(ex.Message) Finally If RecordsAffected > 0 Then MessageBox.Show("Detail Deleted Successfully") clearall() Dgvtrainees.DataSource = Nothing Else MessageBox.Show("Problem in Deleting") End If End Try End If End Sub
Hi Ram,
You have not written statement to execute stored procedure...
try this.. :)
1) You have not erote from in stored procedure.
create proc sptblTrg_detail_Delete @Course_Id int as begin delete from tblTrg_Detail where Trg_Ind_ID= @Course_Id End
2) There is no need to use Sqldataadapter for delete records in database you can use ExecuteNonQuery inseated it
这篇关于删除commad不能正常工作VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!