如何在ASP.NET SQL Server中删除行 [英] How to delete a row in asp.net sql server

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

问题描述

你好

我有一个存储过程,其中有一条删除语句,如

从id = @ id
的表中删除
现在我想知道如何在c#asp.net中删除一行.

这是什么代码.
请帮助

Hello

I have a stored procedure in which i have a delete statement as

delete from table where id=@id

now i want to know how to delete a row in c# asp.net.

what is the code for this.
Plz help

推荐答案

执行存储过程:
Either execute the stored procedure:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("myProcedureName", con))
        {
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.AddWithValue("@Param1", "My Parameter 1 value");
        com.Parameters.AddWithValue("@Param2", "My Parameter 1 value");
        com.Parameters.AddWithValue("@Param3", "My Parameter 1 value");
        com.ExecuteNonQuery();
        }
    }



或仅将其作为代码执行:



or just execute it as code:

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("DELETE FROM table WHERE id=@ID", con))
        {
        com.Parameters.AddWithValue("@ID", idToDelete);
        com.ExecuteNonQuery();
        }
    }


请参阅以下代码:
Refer this code:
SqlConnection connection = new System.Data.SqlClient.SqlConnection(strConnections);
string Command = "select * from tableName WHERE ID = " + txtID.Text.Trim() + "";
SqlDataAdapter dataAdapter2 = new SqlDataAdapter(Command, strConnections);
System.Data.SqlClient.SqlCommand oCommand = new System.Data.SqlClient.SqlCommand();
connection.Open();
SqlTransaction transaction = connection.BeginTransaction();
oCommand.Connection = connection;
oCommand.Transaction = transaction;
oCommand.CommandText = "deleteVendPymnt";
oCommand.CommandType = CommandType.StoredProcedure;
dataAdapter2.UpdateCommand = oCommand;
dataAdapter2.UpdateCommand.Transaction = transaction;
try
{
 oCommand.ExecuteNonQuery();
 transaction.Commit();
 connection.Close();
 MessageBox.Show("Delete succeed!", "Delete option", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch
{
 transaction.Rollback();
 MessageBox.Show("Concurrency error!!!", "Delete option", MessageBoxButtons.OK,  MessageBoxIcon.Stop);
}



另请参阅类似的答案:
使用存储过程插入和删除数据库 [



Also refer similar answer:
Database Insert and Delete with Stored Procedure[^]


select-insert-update -delete-using-stored-procedure-in-sql/ [

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

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