如何从表中删除

编程入门 行业动态 更新时间:2024-10-21 19:49:06
本文介绍了如何从表中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嘿,我想删除一个选定的工作人员(显示在列表框中)/我的代码

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 = 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:-)

更多推荐

如何从表中删除

本文发布于:2023-10-27 23:19:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534828.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!