任何人都可以将此SQL查询更改为mysql格式 [英] Can anyone change this SQL query to mysql format

查看:87
本文介绍了任何人都可以将此SQL查询更改为mysql格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从两个用户之间的对话中检索所有消息。以前我使用SQL数据库现在我已经将数据库从SQL更改为MySQL。现在,这不行。



我尝试过:



I'm trying to retrieve all messages from a conversation between two users. Previously I was using SQL database now I have changed the database from SQL to MySQL. Now, this is not working.

What I have tried:

public void GetAllMessage(object state)
 {
     LBChat.Items.Clear();
     try
     {
         string messagequery = " SELECT * CLID, SenderID CASE WHEN SenderID = '" + LogInUser.UID + "' AND ReceiverID = '" + ReceiverId.Trim() + "' THEN 'You : ' + Message + ' ( Sent Time : ' + Convert( varchar(30), MessageDateTime ) + ' )' " +
                                 " WHEN ReceiverID =  '" + LogInUser.UID + "'  AND SenderID = '" + ReceiverId.Trim() + "' THEN 'Friend : '+ Message + ' ( Sent Time : ' +  Convert(varchar(30), MessageDateTime ) + ' )' " +
                                 " END " +
                                 " AS [Message] " +
                                 " , ReceiverID , MessageDateTime, UserIPAddress  " +
                                 " FROM chatlogs WHERE SenderID =  '" + LogInUser.UID + "'  AND ReceiverID = '" + ReceiverId.Trim() + "' OR ReceiverID =  '" + LogInUser.UID + "'  AND SenderID = '" + ReceiverId.Trim() + "' order by CLID; ";

         DataTable dt = DataBaseAccess.Retrive(messagequery);
         if (dt != null)
         {
             if (dt.Rows.Count > 0)
             {
                 foreach (DataRow item in dt.Rows)
                 {
                     LBChat.Items.Add(Convert.ToString(item[2]));
                 }
             }
         }
     }
     catch
     {

     }
 }

推荐答案

只是一个不幸的消息,你的查询崩溃了!并且不幸的是问题最少,可以故意制作消息来杀死你的数据库。

Just an unfortunate ' in the message and your query crashes! and unfortunate is the least problem, message can be intentionally crafted to kill your database.
string messagequery = " SELECT * CLID, SenderID CASE WHEN SenderID = '" + LogInUser.UID + "' AND ReceiverID = '" + ReceiverId.Trim() + "' THEN 'You : ' + Message + ' ( Sent Time : ' + Convert( varchar(30), MessageDateTime ) + ' )' " + ...



不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]

我该怎么办?解释没有技术术语的SQL注入? - 信息安全堆栈交换 [ ^ ]


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]


这篇关于任何人都可以将此SQL查询更改为mysql格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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