Checking if a datatable is null

后端 未结 7 1910
南旧
南旧 2021-02-19 22:02

The following code is what I\'ve been using to retrieve user information from a sql database.

            string userName = LoginUser.UserName;
            strin         


        
7条回答
  •  别跟我提以往
    2021-02-19 22:20

    Why not just change the statement a bit to see if the DataTable is either null or has no rows:

    if(dt != null && dt.Rows.Count > 0)
    

    Also, on a side note, you should look into Parameterized Queries as well rather than building your SQL dynamically. It will reduce the number of attack vectors for attackers trying to compromise your application.

提交回复
热议问题