-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Fix null reference exception in setter that sets DbConnection property of the TDengineCommand class. #90
Conversation
var command = new TDengineCommand() { CommandText = "...", CommandType = CommandType.Text, }; var connection = new TDengineConnection("..."); command.DbConnection = connection; 如上代码片段中最后一行代码,因为 TDengineConnection 尚未打开,所以其内部的 client 变量为空,故而导致 TDegnineCommand.DbConnection 属性设置器中的代码引发了空引用异常。 现将 TDengineCommand 类中的 DbConnection 属性的设置器中的 _connection.client.StmtInit() 调用移动到 Statement() 方法中,将 StmtInit() 方法后置到执行方法也更为合理。
添加对 DbParameter.Value 为 DBNull.Value 的支持。
Can someone please help me with the code pull request approvals? |
@PopeyeZhong Can you add some unit tests to cover it? |
@PopeyeZhong Please rebase the 3.0 branch. |
…Query_WithDBNull 两个单元测试用例。
Okay, I've added two unit test cases for TDengineCommand, please check them:
|
YES, I just checked and it's based on the 3.0 branch. |
public void SetConnection_DoesNotThrowException() | ||
{ | ||
using(var command = new TDengineCommand()) | ||
{ | ||
command.CommandText = "SELECT * FROM t"; | ||
var ex = Record.Exception(() => command.Connection = _connection); | ||
Assert.Null(ex); | ||
} | ||
|
||
using(var command = new TDengineCommand()) | ||
{ | ||
using(var connection = new TDengineConnection("username=root;password=taosdata")) | ||
{ | ||
command.CommandText = "SELECT * FROM t"; | ||
var ex = Record.Exception(() => command.Connection = connection); | ||
Assert.Null(ex); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ExecuteNonQuery_WithDBNull() | ||
{ | ||
using(var command = new TDengineCommand(_connection)) | ||
{ | ||
command.CommandText = "create table if not exists t_dbnull (ts timestamp,v int,description nchar(100))"; | ||
command.ExecuteNonQuery(); | ||
command.CommandText = "INSERT INTO t_dbnull VALUES (?,?,?)"; | ||
command.Parameters.AddWithValue(DateTime.Now); | ||
command.Parameters.AddWithValue(123); | ||
command.Parameters.AddWithValue(DBNull.Value); | ||
|
||
int affectedRows = command.ExecuteNonQuery(); | ||
|
||
Assert.Equal(1, affectedRows); | ||
} | ||
} | ||
|
||
[Fact] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please format the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的哈~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
format the test code
Done! please check it. |
var command = new TDengineCommand()
{
CommandText = "...",
CommandType = CommandType.Text,
};
var connection = new TDengineConnection("...");
command.DbConnection = connection;
上述代码片段中最后一行代码,因为 TDengineConnection 尚未打开,所以其内部的 client 变量为空,故而导致 TDegnineCommand.DbConnection 属性设置器中的代码引发了空引用异常。
现将 TDengineCommand 类中的 DbConnection 属性的设置器中的 _connection.client.StmtInit() 调用移动到 Statement() 方法中,将 StmtInit() 方法后置到执行方法也更为合理。