English 中文(简体)
页: 1 服务器数据库查询
原标题:SQL Server database query

我已经使用标准用户表,即ASP.net的安装和Im的电离层能够删除用户。 首先,我需要删除一个叫作成员的表格中的用户,然后删除用户。 为此,我设立了2个文本箱,一个供用户使用,另一个供用户使用。

T-SQL声明的任何想法,将首先删除用户,然后移至删除用户名称。

else
{
   try
   {
      connection.Open();
      cmd = new SqlCommand("DELETE from Membershio
                            WHERE UserId = " + deleteuserIDbox.Text + " ", connection);
      cmd = new SqlCommand("DELETE from Users WHERE UserName = " + deleteuserbox.Text + " ", connection);
      cmd.ExecuteNonQuery();
      update.Text = "Your data has been removed";
   }
   catch
   {
      update.Text = "Your data has not been deleted";
   }
}

这两张表格是相关的,因此,我需要首先删除用户名称,然后删除用户名称。

任何帮助

最佳回答

如果理解正确的话,你的输入方法有严重问题。

例如,

UserID   UserName
 1        testUser
 2        testUser2 

根据您的申请逻辑,我可以将“1”输入删除用户信息箱和“测试User2”进入删除用户箱,然后将用户信息数据库1移至而不是

如果你没有这样做的话,你需要利用用户信息数据库的外在钥匙链接这两个表格。 因此,与用户信息数据库领域的联系一直存在。

还有一个问题是,你正在利用用户的投入直接执行询问,从而使 s喷射的档案成为可能。

关于您的询问,你可以在两厘米的发言之间“立 cm见影”。

问题回答

为了使用你目前的法典,你将需要执行第一个询问,然后为第二次查询确定指挥系统,执行。

  using (SqlCommand cmd = connection.CreateCommand())
  {
       cmd.CommandText = "DELETE FROM Membership WHERE UserID = @UserID";

       cmd.Parameters.AddWithValue("@UserID", deleteuserIDbox.Text);

       connection.Open();

       cmd.ExecuteNonQuery();

       cmd.Paramters.Clear();

       cmd.CommandText = "DELETE from Users WHERE UserName = @UserName";

       cmd.Parameters.AddWithValue("@UserName", deleteuserbox.Text);

       cmd.ExecuteNonQuery();
  }

另一种选择是采用一种储存程序,使你能够一起处理这两个问题。

另一种选择是做临时删除。 http://rudesyle.wordpress.com/2008/01/28/cascading-deletes-in-sql-server/“rel=“nofollow”>aLink。

最后,您正在开放至SQL Injection。 您应当从用户那里获取信息,并将数据整理成电子数据表。 您要么采用储存程序,要么采用参数化查询(如上表)。

You re not executing the first command:

connection.Open();

cmd = new SqlCommand("DELETE from Membershio
      WHERE UserId = " +
      deleteuserIDbox.Text + " ", connection);

cmd.ExecuteNonQuery();

cmd = new SqlCommand("DELETE from Users WHERE
      UserName = " + deleteuserbox.Text +
      " ", connection);

cmd.ExecuteNonQuery();

此外,这些指挥应在交易中执行。

很晚,但我今天只注意到你的问题。

通过在数据库中这样做,你正在绕过一切好战! 您应在C#中通过呼吁成员:DeleteUser方法学。

你们不应听从会员制度的内部。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签