且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.NET SQL Server中删除行

更新时间:2023-02-06 23:05:22

执行存储过程:
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/ [