如何从表中删除 [英] How Do I Delete From A Table
本文介绍了如何从表中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void button2_Click( object sender,EventArgs e)
{
SqlConnection con = new SqlConnection( 数据源= JAMES-PC\\SQLEXPRESS;初始目录=员工;集成安全性=真);
con.Open();
string sql = @ DELETE FROM staff1其中Id = @ name;;
SqlCommand cmd = new SqlCommand(sql,con);
cmd.Parameters.AddWithValue( @ name, 1 跨度>);
cmd.ExecuteNonQuery();
con.Close();
}
但是当我运行程序时,我点击按钮(删除)但是没有运气,没有删除它
任何帮助将不胜感激
列表框中显示的表格是(名称)
解决方案
private void button2_Click( object sender,EventArgs e)
{
SqlConnection con = new SqlConnection( 数据源= JAMES-PC\\SQLEXPRESS;初始目录=员工;集成安全性=真);
con.Open();
string sql = @ DELETE FROM staff1其中Id = @ name;;
SqlCommand cmd = new SqlCommand(sql,con);
cmd.Parameters.AddWithValue( @ name, listBox1.SelectedValue。的ToString()的);
cmd.ExecuteNonQuery();
con.Close();
}
看看参数id是否为varchar,它是这样的:
DELETE FROM staff1其中Id ='staff1';
转到Web Config并粘贴此
< connectionStrings >
< add name = constring connectionString = 数据source = JAMES-PC\\SQLEXPRESS; initial catalog = staff; persist security info = true; integrated security = true 的providerName 跨度> < span class =code-keyword> = System.Data.SqlClient / >
< / connectionStrings >
和chane代码如下
private void button2_Click(object sender,EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [constring ] .ConnectionString);
con.Open();
string sql = @DELETE FROM staff1 where name = @ name;;
SqlCommand cmd = new SqlCommand(DELETE FROM staff1 where name = @ name,con);
cmd.Parameters.AddWithValue(@ name,1);或//cmd.Parameters.AddWithValue(\"@name,listBox1.SelectedItem.ToString());
cmd.ExecuteNonQuery();
con.Close();
}
始终带id进行删除。谢谢: - )
hey , i want to delete a selected "staff member" (displayed in listbox) / my code
private void button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=JAMES-PC\\SQLEXPRESS;Initial Catalog=staff;Integrated Security=True");
con.Open();
string sql = @"DELETE FROM staff1 where Id=@name;";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@name", 1);
cmd.ExecuteNonQuery();
con.Close();
}
but when i run the program i click the button (remove) but no luck, doesnt delete it
any help would be appreciated
also the table displayed in the listbox is (name)
解决方案
private void button2_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=JAMES-PC\\SQLEXPRESS;Initial Catalog=staff;Integrated Security=True"); con.Open(); string sql = @"DELETE FROM staff1 where Id=@name;"; SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.AddWithValue("@name", listBox1.SelectedValue.ToString()); cmd.ExecuteNonQuery(); con.Close(); }
see if the parameter id is varchar it is enclosed like this:
DELETE FROM staff1 where Id= 'staff1';
Go to Web Config and paste this
<connectionStrings> <add name="constring" connectionString="data source=JAMES-PC\\SQLEXPRESS; initial catalog=staff; persist security info= true; integrated security= true" providerName="System.Data.SqlClient"/> </connectionStrings>
and chane the code as follow
private void button2_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString); con.Open(); string sql = @"DELETE FROM staff1 where name=@name;"; SqlCommand cmd = new SqlCommand("DELETE FROM staff1 where name=@name", con); cmd.Parameters.AddWithValue("@name", 1); or //cmd.Parameters.AddWithValue("@name", listBox1.SelectedItem.ToString()); cmd.ExecuteNonQuery(); con.Close(); }
Always go with id for deletion.Thank You:-)
这篇关于如何从表中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文