如何使用SQL Server 2005删除ASP.net中的记录 [英] How to delete a record in ASP.net using SQL Server 2005

查看:71
本文介绍了如何使用SQL Server 2005删除ASP.net中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题.目前,我正在阅读有关如何删除记录的所有教程,但仍然无法理解.请帮助我找到一个站点或有关如何使用/不使用gridview删除它的示例...感谢和更多的力量

hi guys, ive got a problem. Currently im reading all the tutorials regarding on how to delete a record but still can''t understand it. Plss help me find a site or examples on how to delete it using/without gridview... thanks and more power

推荐答案

网上有各种各样的链接

看到此链接

http://www.dotnetspark.com/kb/643-how-to-editupdatedelete- gridview.aspx [ ^ ]
variuos links are there on net

see this link

http://www.dotnetspark.com/kb/643-how-to-editupdatedelete-gridview.aspx[^]


创建命令对象,编写删除查询,然后使用命令对象执行查询.还有许多其他方法.但是我不认为这正是您真正想要的.您需要更具体并正确描述您的情况,以获得更好的答案.
更新:
OP写道:先生,有一个名为DELETE的按钮,当我使用gridview突出显示记录并单击该按钮时,它将被自动删除.可能吗?现在我只能通过gridview查看它
您可以使用Google/Bing轻松找到示例.这里有几个:
MSDN-ASP.NET 2.0的GridView示例:删除GridView的基础数据 [ ^ ]
CodeProject-带有确认的GridView删除 [ CodeProject-插入,更新,使用Gridview删除....简单的方法 [ [
Create a command object, write a delete query and execute the query using command object. There are many other ways. But I don''t think this is what you exactly want. You need to more specific and describe your scenario properly to get a better answer.
UPDATE:
OP wrote: Sir There is one button named DELETE and when i highlighted a record using a gridview and click the button, it will be automatically deleted. Is it possible? Now i can only view it via gridview
You could have easily found the examples using Google/Bing. Here are few:
MSDN - GridView Examples for ASP.NET 2.0: Deleting a GridView''s Underlying Data[^]
CodeProject - GridView Delete, with Confirmation[^]
CodeProject - Insert, Update, Delete with Gridview....simple way[^]

I found all of them by doing this[^].


不使用gridview.


Without using gridview..



''Add following Namespace
using system.data;
using syste.data.sqlclient;

''Write on delete buttons click event

string query = "delete from tablename where primarykeycolumnname = value";
SQLConnection con = new sqlConnection(''your connection string);
SQLCommand cmd = new SQLCommand(query, con);

try
{
    con.Open();
    cmd.ExecuteNonQuery();
}
catch
{
   ''Handle Exception
}
finally
{
     con.Close();
}



对于gridview,在rowdeleting事件中编写以上代码

但查询中的primarykeyvalue将来自以下代码

首先将gridview的datakeys属性设置为主键值

现在访问相同的内容,例如说ID是主键
因此您可以在rowdeletingevent中将其值获取为

int值= gridview1.Items [gridview1.selectedIndex] .value.tostring();

遵循上面的代码



and for gridview write the above code in the rowdeleting event

but the primarykeyvalue in the query will come from the follwing code

first of all set the datakeys properties of the gridview to primary key value

and now access the same for eg say ID is the primary key
so you can get its value in rowdeletingevent as

int value = gridview1.Items[gridview1.selectedIndex].value.tostring();

following the code above


这篇关于如何使用SQL Server 2005删除ASP.net中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆