删除查询不起作用 [英] delete query not working
问题描述
如何在提交按钮点击时删除gridview数据
我的网页上有gridview和一个按钮。
当页面加载时,数据显示在gridview上。
因此,当我点击提交按钮时,我想从表中删除完整数据。
因此无法在gridview中显示。
这是我的代码
SqlConnection conDelete = new SqlConnection(_connString);
SqlCommand cmdDelete = new SqlCommand();
cmdDelete.Connection = conDelete;
cmdDelete.CommandText =从学生中删除;
使用(cmdDelete)
{
conDelete.Open();
cmdDelete.ExecuteNonQuery();
conDelete.Close();
}
这个不起作用请帮忙。
谢谢
您需要为 sqlcommand [ ^ ],如下所示:
SqlConnection conDelete = new SqlConnection(_connString);
conDelete.Open();
SqlCommand cmdDelete = new SqlCommand( DELETE来自学生WHERE StudentId = 1,conDelete);
int retVal = cmdDelete.ExecuteNonQuery();
// retval存储受影响的受访者的价值
请记住,您需要将记录范围定义为 DELETE( T-SQL) [ ^ ],但如果要删除所有数据,则不需要WHERE语句:
DELETE
FROM 学生
以上查询与 TRUNCATE TABLE(T-SQL) [ ^ ]。
使用此: -
TRUNCATE TABLE 学生
祝你好运
试试这个: -
Dim con As 新 SqlConnection
Dim cmd 作为 新 SqlCommand
尝试
con.ConnectionString = Data Source = atisource; Initial Catalog = BillingSys; Persist Security Info = True; User ID = sa; Password = 12345678
con.Open()
cmd.Connection = con
cmd.CommandText = DELETE FROM table_name
cmd.ExecuteNonQuery()
Catch ex As 例外
MessageBox.Show( deletin时出错g记在桌子上......& ex.Message, 删除记录)
最后
con.Close()
结束 尝试
how to delete the gridview data when submit button click
I have gridview and a button on my webpage.
when pageloads, the data displays on gridview.
so when i clicks the submit button i want to delete the complete data from table.
so it cannot be displayed in gridview.
this is my code
SqlConnection conDelete = new SqlConnection(_connString); SqlCommand cmdDelete = new SqlCommand(); cmdDelete.Connection = conDelete; cmdDelete.CommandText = "Delete from students"; using (cmdDelete) { conDelete.Open(); cmdDelete.ExecuteNonQuery(); conDelete.Close(); }
this is not working please help.
thanks
You need to define connection for sqlcommand[^], like this:
SqlConnection conDelete = new SqlConnection(_connString); conDelete.Open(); SqlCommand cmdDelete = new SqlCommand("DELETE FROM Students WHERE StudentId=1", conDelete); int retVal = cmdDelete.ExecuteNonQuery(); //retval stores value for recoreds affected
Remember, you need to define records range to DELETE (T-SQL)[^], but if you want to delete all data, WHERE statement is not needed:
DELETE FROM Students
Above query differs from TRUNCATE TABLE (T-SQL)[^].
Use this :-
TRUNCATE TABLE students
Good luck
try for this:-
Dim con As New SqlConnection Dim cmd As New SqlCommand Try con.ConnectionString = "Data Source=atisource;Initial Catalog=BillingSys;Persist Security Info=True;User ID=sa;Password=12345678" con.Open() cmd.Connection = con cmd.CommandText = "DELETE FROM table_name" cmd.ExecuteNonQuery() Catch ex As Exception MessageBox.Show("Error while deleting record on table..." & ex.Message, "Delete Records") Finally con.Close() End Try
这篇关于删除查询不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!