Checking if a datatable is null

后端 未结 7 1908
南旧
南旧 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:17

    It seems to me using a DataTable and SqlDataAdapter is a little bit too heavy for the task.

    You can just use a DataReader here:

            SqlCommand command = new SqlCommand(comm, conn);
            using (var reader = command.ExecuteQuery()) 
            {
                if (reader.Read())
                {
                    //logic
                    var userName = reader.GetString(0);
                    var password = reader.GetString(1);
                    // etc
                }
            }
    

提交回复
热议问题