ASP.Net - How to delete a row using dataset
I need to delete a row from the database. Here's what I've got till now. How to execute the sqlcommand in order to delete the appropriate开发者_开发问答 row
Dim da As New SqlDataAdapter
Dim ds As New DataSet
Dim conSQL As SqlConnection = New SqlConnection
conSQL.ConnectionString = "Data Source=DUSHYANT-PC\SQLEXPRESS;Initial Catalog=Phd;Integrated Security=True"
Dim cmd As New SqlCommand("delete from Phd_Student where student_id = '" + sidnolabel.Text + "'", conSQL)
da.SelectCommand = cmd
You don't need data adapter or data set for this task. You'll only need SqlCommand.
Dim conSQL As SqlConnection = New SqlConnection
conSQL.ConnectionString = "Data Source=DUSHYANT-PC\SQLEXPRESS;Initial Catalog=Phd;Integrated Security=True"
conSQL.Open()
Dim cmd As New SqlCommand("delete from Phd_Student where student_id = '" + sidnolabel.Text + "'", conSQL)
cmd.ExecuteNonQuery()
If you are really inclined to use datasets you may use Delete method for deleting. It will mark a record in your dataset as deleted. After this you'll need to call AcceptChanges to apply changes to DB.
Note, that passing arguments using string concatenation is a very bad practice, possibly leading to SQL injections. Use parameters instead.
精彩评论