有人可以请教我这些代码吗? [英] Can someone please enlighten me on these codes?

查看:102
本文介绍了有人可以请教我这些代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string sqlCM = @"INSERT INTO Cancelled_Meeting (meeting_id, date, room_id, attendees_pax, start_time, end_time)
                           (SELECT * from meeting WHERE meeting_id = {1})";
           int db = DBUtl.ExecSQL(sqlCM, TxtMeetingID.Text, TxtDate.Text, DrpRoom.SelectedValue, DrpPax.SelectedValue, ti.Value, to.Value);

           if (db == 1)
           {
               LtlMsg.Text = "Added into Cancelled Meeting";
           }
           else
           {
               LtlMsg.Text = "Error " + DBUtl.DB_Message;
           }





我尝试过:



它一直说操作数冲突,int与日期不兼容的事情是我没有为日期设置格式。



What I have tried:

it keeps saying that "operand clash, int not compatible with date" the thing is I did not set a format for the date yet.

推荐答案

从不使用 SELECT * FROM 作为SQL查询开始 - 特别是当您需要对值进行特定订购时。始终列出列名称,或者返回的行可能不是您期望的顺序,然后您会遇到类型冲突。

尝试:

Start by never using SELECT * FROM as an SQL query - particularly when you need a specific order to your values. Always list the column names, or the rows returned may not be in the order you expect, and then you get type conflicts.
Try:
INSERT INTO Cancelled_Meeting (meeting_id, date, room_id, attendees_pax, start_time, end_time)
SELECT meeting_id, date, room_id, attendees_pax, start_time, end_time from meeting 
WHERE ...

但是...我担心最后的{0} - 这意味着你要连接字符串以在ExecSQL方法中形成最终的SQL语句,这非常危险 - 它使你的代码对SQL注入保持开放状态可以破坏整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

But ... I worry about that "{0}" at the end - it implies you are going to concatenate strings to form the final SQL statement in your ExecSQL method, and that's very dangerous - it leaves your code wide open to SQL Injection which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?


这篇关于有人可以请教我这些代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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