如何解决对象引用设置为对象C#的实例 [英] How do I solve object reference set to an instance of an object C#

查看:102
本文介绍了如何解决对象引用设置为对象C#的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在Windows窗体应用程序中创建一个新的Ward时出现问题,我的想法是在将病房名称保存到数据库之前让系统检查数据库中是否存在输入的值。数据库。这是完美的,但它不会保存后检查值,我得到一条错误信息说..



我尝试过:



private void button2_Click(object sender,EventArgs e)

{

if(textBox1.Text ==)

{





MessageBox.Show(Ward Name不能留空。 ...,Alert,MessageBoxButtons.OK,MessageBoxIcon.Error);

返回;

}



尝试

{



string che = @(从病房中选择*,其中Ward_Name ='+ textBox1.Text + ');

SqlCommand cmd = new SqlCommand(插入病房(Ward_Name,Ward_Type,Number_Occupant,Number_Bed)值('+ textBox1.Text +','+ comboBox1.Text + ','+ comboBox2.Text +','+ textBox2.Text +'),con);

con.Open();

SqlCommand cmda = new SqlCommand(che,con);



int count =(int )cmda.ExecuteScalar();

if(count> 0)

{

MessageBox.Show(这个病房已经存在);



}

其他

{



cmd.ExecuteNonQuery();

MessageBox.Show( 一个新的Ward成功添加);

}



con.Close();

} < br $>


catch(例外情况)

{



MessageBox.Show(ex .Message);



}



终于{



if(con.State == ConnectionState.Open)

con.Close();





}

解决方案

引用:

如何解决对象引用设置为对象的实例C#



在使用SQL命令之前,需要检查SQL命令是否成功结果。由于您的代码受SQL注入,一个不幸的用户输入足以使其失败,输入可能是恶意的。

 字符串 che =  @ (从病房中选择*,其中Ward_Name =' + textBox1.Text +  '); 
SqlCommand cmd = new SqlCommand( insert进入病房(Ward_Name,Ward_Type,Number_Occupant,Number_Bed)值(' + textBox1.Text + ' ,' + comboBox1.Text + ',' + comboBox2.Text + ',' + textBox2.Text + '),con);



不是你问题的解决方案,但另一个问题你有。

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

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

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

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

PHP:SQL注入 - 手册 [ ^ ]

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


Hi,
I am having issues creating a new Ward in my windows form application, the idea is to get the system check if the entered value exist in the database before saving the ward name into the database. This is done perfectly but it won't save after the value is check and I get an error message saying..

What I have tried:

private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{


MessageBox.Show("Ward Name cannot be left Blank ....", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

try
{

string che = @"(select * from ward where Ward_Name = '" + textBox1.Text + "')";
SqlCommand cmd = new SqlCommand("insert into ward (Ward_Name, Ward_Type, Number_Occupant, Number_Bed) values('" + textBox1.Text + "', '" + comboBox1.Text + "', '" + comboBox2.Text + "','" + textBox2.Text + "')", con);
con.Open();
SqlCommand cmda = new SqlCommand(che, con);

int count = (int)cmda.ExecuteScalar();
if (count > 0)
{
MessageBox.Show("This Ward already exist");

}
else
{

cmd.ExecuteNonQuery();
MessageBox.Show("A new Ward successfully Added");
}

con.Close();
}

catch (Exception ex)
{

MessageBox.Show(ex.Message);

}

finally {

if (con.State == ConnectionState.Open)
con.Close();


}

解决方案

Quote:

How do I solve object reference set to an instance of an object C#


You need to check if your SQL command succeed or not before using its result. Since your code is subject to SQL injection, an unfortunate user input is enough to make it fail, and the input can be malicious.

string che = @"(select * from ward where Ward_Name = '" + textBox1.Text + "')";
SqlCommand cmd = new SqlCommand("insert into ward (Ward_Name, Ward_Type, Number_Occupant, Number_Bed) values('" + textBox1.Text + "', '" + comboBox1.Text + "', '" + comboBox2.Text + "','" + textBox2.Text + "')", con);


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[^]


这篇关于如何解决对象引用设置为对象C#的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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