c#中不接受null对象
本文关键字:对象 null 不接受 | 更新日期: 2023-09-27 18:02:37
当我运行我的代码时,它告诉我adr的对象是null,这是真的,但是为什么当它在相同方法的副本中工作时,它会工作,除了insert而不是select。
代码是这样的:
public City doesExist(string postnr, string navn, City city, SqlConnection con)
{
DatabaseConnection.openConnection(con);
using (var command = new SqlCommand("select Id from [By] where Postnummer='" + postnr + "' and Navn='" + navn + "'", con))
{
command.Connection = con;
SqlDataReader reader = command.ExecuteReader();
if (reader.Read())
{
city.id = reader.GetInt32(0);
city.postnr = postnr;
city.navn = navn;
reader.Close();
return city;
}
reader.Close();
return null;
}
}
public City create(string postnr, string navn, City city, SqlConnection con)
{
DatabaseConnection.openConnection(con);
using (var command = new SqlCommand("insert into [By] (Postnummer, Navn) values ('" + postnr + "', '" + navn + "'); select @@identity as 'identity';", con))
{
object ID = command.ExecuteScalar();
city.id = Convert.ToInt32(ID);
city.postnr = postnr;
city.navn = navn;
return city;
}
}
调用是这样的:
City city = new City();
city = city.doesExist(zip, by, city, connection); // this works fine
if (city == null)
{
// I know that city is null
// tried inserting City city = new City(); same error
city = city.create(zip, by, city, connection); // this is where the null error occours
}
嗯,是的,看:
if (city == null)
{
// If we've got in here, we know that city is a null reference, but...
city = city.create(...);
}
在绝对为null的引用上调用方法。这保证会抛出一个NullReferenceException
您几乎肯定想使您的create
方法静态(并将其重命名以符合正常的。net命名约定),并将其称为
city = City.Create(...);
您还需要从方法调用中删除city
参数,并在方法中创建一个新的City
对象。例如:
public static City Create(string postnr, string navn, SqlConnection con)
{
DatabaseConnection.openConnection(con);
using (var command = new SqlCommand
("insert into [By] (Postnummer, Navn) values (@postnr, @navn); "+
"select @@identity as 'identity';", con))
{
command.Parameters.Add("@postnr", SqlDbType.NVarChar).Value = postnr;
command.Parameters.Add("@navn", SqlDbType.NVarChar).Value = navn;
object ID = command.ExecuteScalar();
City = new City();
city.id = Convert.ToInt32(ID);
city.postnr = postnr;
city.navn = navn;
return city;
}
}
注意我是如何改变你的代码来使用参数化SQL的。你真的,真的,不应该像那样把值直接放进你的SQL语句中——这会使你的系统受到SQL注入攻击,并使各种转换变得混乱。
此外,我建议为每个数据库操作创建一个新的SqlConnection
(并关闭它)。
坦率地说,doesExist
是一个实例方法有点奇怪,太…再一次,它接受一个city
参数。
我建议改变这个设计,这样你就有一个CityRepository
(或类似的东西)知道连接字符串,并暴露:
// I'd rename these parameters to be more meaningful, but as I can't work out what they're
// meant to mean now, it's hard to suggest alternatives.
public City Lookup(string postnr, string nav)
public City Create(string postnr, string nav)
存储库将知道相关的连接字符串,并将负责所有数据库操作。City
类型对数据库一无所知。
您正在尝试调用尚未初始化/为空的对象类的方法。
您需要将City.create
设置为静态成员。
public static City create(string postnr, string navn, City city, SqlConnection con)
{
DatabaseConnection.openConnection(con);
using (var command = new SqlCommand("insert into [By] (Postnummer, Navn) values ('" + postnr + "', '" + navn + "'); select @@identity as 'identity';", con))
{
object ID = command.ExecuteScalar();
city.id = Convert.ToInt32(ID);
city.postnr = postnr;
city.navn = navn;
return city;
}
}
并像这样使用:
if(city==null)
{
City city = City.create(... );
}
更好的方式:
static public City create(string postnr, string navn, SqlConnection con)
{
DatabaseConnection.openConnection(con);
using (var command = new SqlCommand("insert into [By] (Postnummer, Navn) values ('" + postnr + "', '" + navn + "'); select @@identity as 'identity';", con))
{
object ID = command.ExecuteScalar();
City city = new City();
city.id = Convert.ToInt32(ID);
city.postnr = postnr;
city.navn = navn;
return city;
}
return null;
}
Create方法必须是静态的,不需要有city in参数。调用它:
if (city == null)
{
city = City.Create(.....);
}