如何在1个查询中合并3个条件 [英] How to merge 3 conditions at 1 query

查看:90
本文介绍了如何在1个查询中合并3个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



如何在1个查询中加入3个条件,以便弹出消息框说3条件满了?



如果NewData则
strSQL =
SELECT Count(tblContact.Fullname,)AS CountFullname FROM tblContact&
WHERE Fullname =& '& txtFullname.Text& '





SELECT Count(tblContact.Phone,)AS CountPhone FROM tblContact& 
WHERE Phone =& '& txtPhone.Text& '







SELECT Count(tblContact。电子邮件,)AS Countemail FROM tblContact& 
WHERE email =& '& txtemail.Text& '





我尝试过:



论坛,从句,示例代码。

解决方案

这与如何在VB中添加3个IF条件 - Visual Basic讨论板 [ ^ ] 。我注意到你仍然没有使用参数化查询,所以你的数据库仍然存在腐败或更糟的风险。


我也修复了你的SQL注入问题....你可能需要调整语法因为我的IDE都被占用所以我正在写这个直接

 StringBuilder sb =  new  Stringbuilder(  SELECT); 
sb.AppendLine( NameCount =(SELECT Count(1)FROM tblContact WHERE FullName = @FullName) );
sb.AppendLine( ,PhoneCount =(SELECT Count(1)FROM tblContact WHERE Phone = @Phone) );
sb.AppendLine( ,EmailCount =(SELECT Count(1)FROM tblContact WHERE Email = @Email) );


string strSQL = sb.ToString();
SqlCommand cmd = new SqlCommand(strSql, 连接);
cmd.Parameters.AddWithValue( @ FullName,txtFullname.Text);
cmd.Parameters.AddWithValue( @ Phone,txtPhone.Text);
cmd.Parameters.AddWithValue( @ Email,txtemail.Text);


Hello,

How to join 3 conditions at 1 query so messagebox pops up to say 3 condition is fullfilled ?

If NewData Then
        strSQL =
            " SELECT Count(tblContact.Fullname, ) AS CountFullname FROM tblContact " &
            " WHERE Fullname = " & "'" & txtFullname.Text & "'"



" SELECT Count(tblContact.Phone, ) AS CountPhone FROM tblContact " &
     " WHERE Phone = " & "'" & txtPhone.Text & "'"




" SELECT Count(tblContact.email, ) AS Countemail FROM tblContact " &
     " WHERE email = " & "'" & txtemail.Text & "'"



What I have tried:

Forums, clauses, sample codes.

解决方案

This is basically the same issue as How to add 3 IF conditions at VB - Visual Basic Discussion Boards[^]. And I note you are still not using parameterised queries, so your database is still at risk of corruption or worse.


I fixed your SQL Injection problem too.... You may have to adjust syntax as my IDE's are all occupied so I'm writing this direct

StringBuilder sb = new Stringbuilder("SELECT ");
sb.AppendLine("NameCount = (SELECT Count(1) FROM tblContact WHERE FullName = @FullName)");
sb.AppendLine(", PhoneCount = (SELECT Count(1) FROM tblContact WHERE Phone = @Phone)");
sb.AppendLine(", EmailCount = (SELECT Count(1) FROM tblContact WHERE Email = @Email)");


string strSQL = sb.ToString();
SqlCommand cmd = new SqlCommand(strSql, "connection");
cmd.Parameters.AddWithValue("@FullName", txtFullname.Text);
cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
cmd.Parameters.AddWithValue("@Email", txtemail.Text);


这篇关于如何在1个查询中合并3个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆