嗨,如何从SQL表中删除记录? [英] hi how to delete records from sql table ?
本文介绍了嗨,如何从SQL表中删除记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi,当我在文本框中显示值时,如何删除sql表中的记录;当我按下Delete按钮时,应该从sql表中删除..任何人都可以帮助我如何查询该查询吗?
??
hi how to delete records from sql table when i show values in textbox and when i hit delete button record should delete from sql table..can any one help me how to give query for that
??
推荐答案
使用
SQL删除
DevGuru Quick reference.
Beginners hate this, but I must repeat this mantra:
Do a Google search first.
private void Delete ( )
{
try
{
if (TxtEmpcode.Text == "")
{
MessageBox.Show( "Please Enter the valid Employee Code", "CURD", MessageBoxButtons.OK, MessageBoxIcon.Information );
TxtEmpcode.Focus ( );
TxtEmpcode.Text = "";
TxtEmpName.Text = "";
TxtQual.Text = "";
TxtCom.Text = "";
TxtAdd.Text = "";
}
else
{
//before delete validate the requested id is there or not
objconnection = new SqlConnection ( WebConfigurationManager.ConnectionStrings["Conn"].ConnectionString );
SqlCommand objcommand = new SqlCommand ( );
objcommand.Connection = objconnection;
objcommand.CommandType = CommandType.Text;
objcommand.CommandText = "Select Count(*) from Employee where EmpCode=" + TxtEmpcode.Text.Trim ( ) + "";
objconnection.Open ( );
int val = int.Parse ( objcommand.ExecuteScalar ( ).ToString ( ) );
if (val > 0)
{
//delete record here
objconnection = new SqlConnection ( WebConfigurationManager.ConnectionStrings["Conn"].ConnectionString );
objcommand = new SqlCommand ( );
objcommand.Connection = objconnection;
objcommand.CommandType = CommandType.Text;
objcommand.CommandText = "Delete from Employee where EmpCode=" + TxtEmpcode.Text.Trim ( ) + "";
objconnection.Open ( );
objcommand.ExecuteNonQuery ( );
objconnection.Close ( );
MessageBox.Show ( " Employee record is successfully Deleted", "CURD", MessageBoxButtons.OK, MessageBoxIcon.Information );
TxtEmpcode.Focus ( );
TxtEmpcode.Text = "";
TxtEmpName.Text = "";
TxtQual.Text = "";
TxtCom.Text = "";
TxtAdd.Text = "";
}
else
{
MessageBox.Show ( "Please Enter the valid Employee Code", "CURD", MessageBoxButtons.OK, MessageBoxIcon.Information );
MessageBox.Show ( "Employee record is successfully deleted" + TxtEmpcode.Text, "CURD", MessageBoxButtons.OK, MessageBoxIcon.Information );
TxtEmpcode.Focus ( );
TxtEmpcode.Text = "";
TxtEmpName.Text = "";
TxtQual.Text = "";
TxtCom.Text = "";
TxtAdd.Text = "";
}
}
}
catch (Exception ex)
{
MessageBox.Show ( "error occur", "CURD", MessageBoxButtons.OK, MessageBoxIcon.Error );
}
finally
{
objconnection.Close();
}
}
这篇关于嗨,如何从SQL表中删除记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文