我有一个组合框,它正在查看我的数据库并向我显示用户的名字,我还想查看除此之外的dID,我发现很难实现这个任务。我有两个表,所以我假设我必须重复这个SQL语句。请查看此代码!
总而言之,我想要实现的只是从数据库中删除用户!会员个人信息表格table_1以及有关会员付款金额和年份dID的信息构成了table_2!一个删除命令删除table_1&中成员信息的一行。 TABLE_2。
欢迎任何其他方法或想法!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CemiyetAidatSistem
{
public partial class DeleteUser : Form
{
String conStr ="My connection string";
public UyeSil()
{
InitializeComponent();
}
SqlDataAdapter da;
DataSet ds = new DataSet();
private void UserDelete_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT FullName FROM Members";
da = new SqlDataAdapter(cmd);
da.Fill(ds, "Members");
cmbUyeSil.DataSource = ds.Tables["Members"];
cmbUyeSil.DisplayMember = "DistinctID";
cmbUyeSil.ValueMember = "FullName";
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "DELETE FROM Members WHERE ID = '" + cmbUyeSil.SelectedValue + "'";
da = new SqlDataAdapter(cmd);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show(" Member with dID \"" + cmbUyeSil.SelectedValue + "\"is deleted");
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
答案 0 :(得分:1)
如果您有cascading delete,那么当您删除用户时,这将导致表2中的行也被删除
答案 1 :(得分:0)
SqlCommand delcomm = new SqlCommand();
delcomm.CommandType = CommandType.StoredProcedure; //I'd do it with an sp, up to you
delcomm.CommandText = "sp_delUser";
SqlParameter delParam = new SqlParameter("@dID", SqlDbType.Int);//I'm assuming this is the column you need and the right datatype
delcomm.Parameters.Add(delParam);
da.DeleteCommand = delcomm;
da.AcceptChangesDuringUpdate = false;
da.Update(UserTable);//you'll need to set up a DataTable with the right data structure
UserTable.AcceptChanges();
这完全脱离了我的头脑;你可能需要稍微改变一下,但如果我理解你的问题,通常你需要做的事情。按照建议对表进行级联删除也是一个好主意。