我该如何纠正错误 [英] How can I correct the error
问题描述
protected void gdv()
{
con.Open();
SqlCommand cmd = new SqlCommand(select * from complaint_table其中username ='+ uname +'),con);
SqlDataAdapter d = new SqlDataAdapter(cmd);
d.Fill(dt); //此部分显示错误
GridView1.DataSource = dt; //此部分显示错误
GridView1.DataBind();
con.Close( );
}
我的尝试:
i尝试很多方式我没有得到解决方案
Quote:我如何纠正错误
有一条错误信息,很高兴告诉我们。
d。填充(dt); // 此部分显示错误
一个人可以此时dt
未定义。
SqlCommand cmd = < span class =code-keyword> new SqlCommand( 从complaint_table中选择*,其中username =' + uname + '),
不是你问题的解决方案,而是你遇到的另一个问题。
永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。
名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。
SQL注入 - 维基百科 [ ^ ]
SQL注入 [ ^ ]
按示例进行SQL注入攻击 [ ^ ]
PHP:SQL注入 - 手册 [ ^ ]
SQL注入预防备忘单 - OWASP [ ^ ]
protected void gdv()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from complaint_table where username= '" + uname + "')", con);
SqlDataAdapter d= new SqlDataAdapter(cmd);
d.Fill(dt);// this portion shows error
GridView1.DataSource = dt;// this portion shows error
GridView1.DataBind();
con.Close();
}
What I have tried:
i try many way i didnt get solution
Quote:How can I correct the error
There is an error message, would be nice to tell us.
d.Fill(dt);// this portion shows error
One can suspect thatdt
is undefined at this point.
SqlCommand cmd = new SqlCommand("Select * from complaint_table where username= '" + uname + "')",
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[^]
这篇关于我该如何纠正错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!